In Android, alterdialog is used. There are two types of alterdialog: one is to use it directly, and the other is to design a layout and display it as a dialog.
1. Define two buttons, one for displaying alterdialog and the other for Displaying Custom alterdialog.
private Button bt1;private Button bt2;
Register an event
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 displays normal alterdialog, and bt2 displays its own alterdialog.
class OnClickEvent implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v == bt1){ShowDialog(MainActivity.this);}if(v == bt2){showDialog_Layout(MainActivity.this);}}}
3. The showdialog method displays the dialog with three buttons. They are OK, cancle, and reset. And click events of the three buttons.
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() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsetTitle("You click OK");}});build.setNeutralButton("Reset", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsetTitle("You click Reset");}});build.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsetTitle("You click Cancle");}});build.show();}
Showdialog_layout displays the custom alterdialog, and defines two buttons: OK and cancle, and their respective click events.
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() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsetTitle(txt.getText());}}); build.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsetTitle("");}}); build.show();}
The UI code of the custom mydialoglayout. XML is:
<?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>
This UI is customized and can be defined more complicated. An edittext file is added to the file.