Home>

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <edittext android:text=""
  android:id="@ + id/edittext"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:editable="false"
  android:cursorvisible="false" />
 <button android:text="Show list dialog"
  android:id="@ + id/button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
</linearlayout>

array.xml array:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="hobby">
  <item>swimming</item>
  <item>Playing basketball</item>
  <item>Mountaineering</item>
 </string-array>
</resources>

alertdialog class:

package com.ljq.dialog;
import android.app.activity;
import android.app.dialog;
import android.app.alertdialog.builder;
import android.content.dialoginterface;
import android.content.dialoginterface.onclicklistener;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
public class alertdialog extends activity {
 private edittext edittext;
 private final static int dialog=1;
 @override
 public void oncreate (bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  setcontentview (r.layout.main);
  edittext=(edittext) findviewbyid (r.id.edittext);
  button button=(button) findviewbyid (r.id.button);
  button.setonclicklistener (new view.onclicklistener () {
   public void onclick (view v) {
    //show dialog
    showdialog (dialog);
   }
  });
 }
 /**
  * Create list dialog
  * /
 @override
 protected dialog oncreatedialog (int id) {
  dialog dialog=null;
  switch (id) {
  case dialog:
   builder builder=new android.app.alertdialog.builder (this);
   //Set the dialog's icon
   builder.seticon (r.drawable.header);
   //Set the title of the dialog
   builder.settitle ("List dialog");
   //Add button,android.content.dialoginterface.onclicklistener.onclicklistener
   builder.setitems (r.array.hobby, new onclicklistener () {
    public void onclick (dialoginterface dialog, int which) {
     string hoddy=getresources (). getstringarray (r.array.hobby) [which];
     edittext.settext ("You selected:" + hoddy);
    }
   });
   //Create a list dialog
   dialog=builder.create ();
   break;
  }
  return dialog;
 }
}

operation result:

  • Previous Detailed usage of distinct in oracle
  • Next In-depth explanation of constructors in C ++