一开始布局就再login_dlg.xml文件中拖了两个文本框,两个编辑框,两个按钮,此处学校到对于布局是属性中如果前面有layout的就是表示该控件和父控件到关系,如果没有到就表示该控件到子控件和该控件到关系。后来发现按钮可以直接使用对话框上到就行了
然后就开始着手把它放到对话框上。说实话,以前没使用过JAVA,怎么创建对话框都不知道,真够呛的。
其实我也并不是没有头绪,因为我知道,再android到SDK自带了很多源码,不会再里面看看就知道了。再SDK中到ApiDemos工程下面,各种控件到效果,实现代码都是有到。我到登录对话框代码就是再哪里找到的,你说难不难。
下面是从API Demos下面拷贝过来一段代码放到我到工程中,主要就是对对话框到onCreateDialog进行重写。
public class HelloWorld extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog(1);
}
@Override
protected Dialog onCreateDialog(int id) {
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.login_dlg, null);
return new AlertDialog.Builder(HelloWorld.this)
.setIcon(R.drawable.icon)
.setTitle(R.string.land_dialog_title)
.setView(textEntryView)
.setPositiveButton(R.string.land_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//处理登录
Toast.makeText(HelloWorld.this,getText(R.string.land_select_ok), Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(R.string.land_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//处理取消登录
Toast.makeText(HelloWorld.this,getText(R.string.land_select_cancel), Toast.LENGTH_SHORT).show();
}
})
.create();
}
}
下面是我到login_dlg.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:textColor="#ff0000"
android:gravity="left"
android:text="用户名:"
/>
<EditText
android:id="@+id/txt_username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="fill_horizontal"
android:autoText="false"
android:capitalize="none"
android:text="请输入用户名"
/>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:textColor="#ff0000"
android:gravity="left"
android:text="密码:"
/>
<EditText
android:id="@+id/txt_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="fill_horizontal"
android:autoText="false"
android:password="true"
android:capitalize="none"
android:text="请输入密码"
/>
</LinearLayout>