Android第一个UI界面学习:登录到对话框


一开始布局就再login_dlg.xml文件中拖了两个文本框,两个编辑框,两个按钮,此处学校到对于布局是属性中如果前面有layout的就是表示该控件和父控件到关系,如果没有到就表示该控件到子控件和该控件到关系。后来发现按钮可以直接使用对话框上到就行了

然后就开始着手把它放到对话框上。说实话,以前没使用过JAVA,怎么创建对话框都不知道,真够呛的。

其实我也并不是没有头绪,因为我知道,再Android到SDK自带了很多源码,不会再里面看看就知道了。再SDK中到ApiDemos工程下面,各种控件到效果,实现代码都是有到。我到登录对话框代码就是再哪里找到的,你说难不难。

下面是从API Demos下面拷贝过来一段代码放到我到工程中,主要就是对对话框到onCreateDialog进行重写。

  1. public class HelloWorld extends Activity  
  2. {  
  3.     /** Called when the activity is first created. */  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.   
  9.         setContentView(R.layout.main);  
  10.         showDialog(1);  
  11.           
  12.            
  13.     }  
  14.   
  15.     @Override  
  16.     protected Dialog onCreateDialog(int id) {  
  17.          LayoutInflater factory = LayoutInflater.from(this);  
  18.             final View textEntryView = factory.inflate(R.layout.login_dlg, null);  
  19.             return new AlertDialog.Builder(HelloWorld.this)  
  20.                 .setIcon(R.drawable.icon)  
  21.                 .setTitle(R.string.land_dialog_title)  
  22.                 .setView(textEntryView)  
  23.                 .setPositiveButton(R.string.land_dialog_ok, new DialogInterface.OnClickListener() {  
  24.                     public void onClick(DialogInterface dialog, int whichButton) {  
  25.   
  26.                         //处理登录   
  27.                         Toast.makeText(HelloWorld.this,getText(R.string.land_select_ok), Toast.LENGTH_SHORT).show();  
  28.                     }  
  29.                 })  
  30.                 .setNegativeButton(R.string.land_dialog_cancel, new DialogInterface.OnClickListener() {  
  31.                     public void onClick(DialogInterface dialog, int whichButton) {  
  32.   
  33.                         //处理取消登录   
  34.                         Toast.makeText(HelloWorld.this,getText(R.string.land_select_cancel), Toast.LENGTH_SHORT).show();  
  35.                     }  
  36.                 })  
  37.                 .create();  
  38.     }  
  39. }  

下面是我的login_dlg.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:id="@+id/username"  
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"   
  11.     android:layout_marginLeft="20dip"  
  12.     android:layout_marginRight="20dip"  
  13.     android:textColor="#ff0000"      
  14.     android:gravity="left"  
  15.     android:text="用户名:"  
  16.     />  
  17. <EditText    
  18.     android:id="@+id/txt_username"  
  19.     android:layout_width="fill_parent"   
  20.     android:layout_height="wrap_content"   
  21.     android:layout_marginLeft="20dip"  
  22.     android:layout_marginRight="20dip"  
  23.     android:gravity="fill_horizontal"  
  24.     android:autoText="false"  
  25.     android:capitalize="none"  
  26.     android:text="请输入用户名"  
  27.     />  
  28. <TextView    
  29.     android:id="@+id/password"  
  30.     android:layout_width="wrap_content"   
  31.     android:layout_height="wrap_content"   
  32.     android:layout_marginLeft="20dip"  
  33.     android:layout_marginRight="20dip"  
  34.     android:textColor="#ff0000"      
  35.     android:gravity="left"  
  36.     android:text="密码:"  
  37.     />  
  38. <EditText    
  39.     android:id="@+id/txt_password"  
  40.     android:layout_width="fill_parent"   
  41.     android:layout_height="wrap_content"   
  42.     android:layout_marginLeft="20dip"  
  43.     android:layout_marginRight="20dip"  
  44.     android:gravity="fill_horizontal"  
  45.     android:autoText="false"  
  46.     android:password="true"  
  47.     android:capitalize="none"  
  48.     android:text="请输入密码"  
  49.     />  
  50. </LinearLayout>  

相关内容