首先我们先写xml,就一个Button点击事件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="lianxi.retrofit_demo.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="200dp"
android:layout_height="50dp"
android:text="点击"
android:layout_centerInParent="true"
/>
</RelativeLayout>
我们给Button按钮一个点击事件,当我们点击按钮的时候会弹出我们写好的AlerDialog
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
Button btn = (Button) findViewById(R.id.btn);
//点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view1) {
// 通过setView方法可以添加一个自定义布局
View view = View.inflate(MainActivity.this, R.layout.dialog_add, null);
final EditText et_name = (EditText) view.findViewById(R.id.et_name);
final EditText et_pwd = (EditText) view.findViewById(R.id.et_pwd);
// 通过builder可以给dialog设置标题、按钮、内容的信息
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("对话框");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"成功",Toast.LENGTH_SHORT).show();
}
});
builder.setView(view);
builder.show();
}
});
}
}
这个是AlertDialog中我们自定义一个我们想要的xml布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="用户名:"
android:gravity="right"
/>
<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="输入用户名"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密码:"
android:gravity="right"
/>
<EditText
android:id="@+id/et_pwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="输入密码"
/>
</LinearLayout>
</LinearLayout>