用得着这么麻烦
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
这样就行了,dialog.dissmiss()也不用调,注意是 DialogInterface.OnClickListener
package com.hw.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.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_1 = (Button) findViewById(R.id.bt_1);
Button bt_2 = (Button) findViewById(R.id.bt_2);
Button bt_3 = (Button) findViewById(R.id.bt_3);
Button bt_4 = (Button) findViewById(R.id.bt_4);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt_1:
bt1();
break;
case R.id.bt_2:
bt2();
break;
case R.id.bt_3:
bt3();
break;
case R.id.bt_4:
bt4();
break;
default :
break;
}
}
private void bt4() {
// TODO Auto-generated method stub
}
private void bt3() {
// TODO Auto-generated method stub
}
private void bt2() {
// TODO Auto-generated method stub
}
private void bt1() {
//构建AlertDialog
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("警告!");
builder.setMessage("世界上最遥远的距离是没有网络!");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("点击了确定按钮");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("点击了取消按钮");
}
});
//最后一步 一定要记得show出来
builder.show();
}
}