private void createcustomChoiceDialog() {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("自定义对话框!");
// 设置自定义布局
View v = LayoutInflater.from(MainActivity.this).inflate(
R.layout.activity_custom, null);
builder.setView(v);
dialog = builder.create();
// 显示
dialog.show();
editixt = (EditText) v.findViewById(R.id.editText1);
but_deng = (Button) v.findViewById(R.id.but_deng);
but_deng.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = editixt.getText().toString();
Log.d("zzz", name);
Toast.makeText(MainActivity.this, name, 0).show();
dialog.dismiss();
}
});
}
-------------------------------------------------------------------------------------
//自定义的视图文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="28dp"
android:text="用户名" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="40dp"
android:text="密码" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/but_deng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginTop="50dp"
android:text="登录" />
<Button
android:id="@+id/but_zhuce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/but_deng"
android:layout_alignBottom="@+id/but_deng"
android:layout_marginLeft="64dp"
android:layout_toRightOf="@+id/but_deng"
android:text="注册" />
</RelativeLayout>