Use alterdialog in Android

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.