2. MainActivity.java
package org.me.android_dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button dialogButton; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); dialogButton = (Button) findViewById(R.id.dialogButton); final AlertDialog alertDialog = getAlertDialog("这是一个对话框","请选择......"); dialogButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(final View view) { //显示对话框 alertDialog.show(); } }); } private AlertDialog getAlertDialog(String title,String message){ //产生一个Builder物件 Builder builder = new AlertDialog.Builder(MainActivity.this); //设定Dialog的标题 builder.setTitle(title); //设定Dialog的内容 builder.setMessage(message); //设定Positive按钮资料 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //按下按钮时显示快显 Toast.makeText(MainActivity.this, "您按下OK按钮", Toast.LENGTH_SHORT).show(); } }); //设定Negative按钮资料 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //按下按钮时显示快显 Toast.makeText(MainActivity.this, "您按下Cancel按钮", Toast.LENGTH_SHORT).show(); } }); //利用Builder物件建立AlertDialog return builder.create(); } }
3. main.xml(Layout)
"
4. 按下ShowDialog按钮之後出现的画面
沒有留言:
張貼留言