Android 中AlterDialog使用,AlterDialog的使用分为两种,一种是直接使用,一种是自己设计一个Layout,然后当做Dialog显示出来。
1、定义两个Button,一个用来显示AlterDialog,一个显示自定义的AlterDialog。
private Button bt1; private Button bt2;
注册事件
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1 = (Button)this.findViewById(R.id.button1); bt1.setOnClickListener(new OnClickEvent()); bt2 = (Button)this.findViewById(R.id.button2); bt2.setOnClickListener(new OnClickEvent()); }
2、bt1显示普通的AlterDialog,bt2显示自己定义的AlterDialog。
class OnClickEvent implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub if(v == bt1) { ShowDialog(MainActivity.this); } if(v == bt2) { showDialog_Layout(MainActivity.this); } } }
3、ShowDialog方法显示带有三个按钮的Dialog。分别为OK,Cancle,Reset。以及三个Button的单击事件。
private void ShowDialog(Context context) { AlertDialog.Builder build = new AlertDialog.Builder(context); build.setIcon(R.drawable.ic_launcher); build.setTitle("Dialog Title"); build.setMessage("Dialog Message"); build.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub setTitle("You click OK"); } }); build.setNeutralButton("Reset", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub setTitle("You click Reset"); } }); build.setNegativeButton("Cancle", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub setTitle("You click Cancle"); } }); build.show(); }
showDialog_Layout显示自己定义的AlterDialog,定义了两个按钮,分别为OK和Cancle,以及各自的单击事件。
private void showDialog_Layout(Context context) { LayoutInflater inflater = LayoutInflater.from(this); final View view = inflater.inflate(R.layout.mydialoglayout, null); final EditText txt = (EditText)view.findViewById(R.id.editText1); AlertDialog.Builder build = new AlertDialog.Builder(context); build.setIcon(R.drawable.ic_launcher); build.setTitle("Dialog Title"); build.setCancelable(false); build.setIcon(R.drawable.ic_launcher); build.setView(view); build.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub setTitle(txt.getText()); } }); build.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub setTitle(""); } }); build.show(); }
自定义的mydialoglayout.xml的UI代码为:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> </LinearLayout>
这个UI是自定义的,当然可以定义的更复杂。上面就放了一个EditText。