1. 簡單的佈局文件 activity_main.xml java
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mytext" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="60dp" android:text="hello" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="delete" android:id="@+id/mybutton" android:layout_below="@+id/mytext" android:layout_centerHorizontal="true" android:layout_marginTop="55dp" />
2.MainActivityandroid
public class MainActivity extends AppCompatActivity { private Button mybutton = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.mybutton = (Button)super.findViewById(R.id.mybutton); this.mybutton.setOnClickListener(new OnClickListenerImp()); } private class OnClickListenerImp implements OnClickListener { @Override public void onClick(View v) { Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("delete?") //設置標題 .setMessage("Are you sure to delete this message") //顯示信息 .setPositiveButton("delete", //增長一個肯定按鈕 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //單擊事件 } }) .setNeutralButton("see more", //增長普通按鈕 new DialogInterface.OnClickListener() { //設置監聽操做 @Override public void onClick(DialogInterface dialog, int which) { //單擊事件 } }) .setNegativeButton("cancle", //增長取消按鈕 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //單擊事件 } }) .create(); //建立Dialog dialog.show(); //顯示對話框 } } }