我在android中制作了一个简单的警报框程序。现在我必须放置两个按钮“确定”和“取消”但是当我运行程序时它只显示“取消”按钮......我的代码如下:
主.java
public class MainActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here.
Toast.makeText(getApplicationContext(), "well come", 1).show();
}
});
alertDialog.setButton("cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
}
});
// Set the Icon for the Dialog
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
// see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button
}
});
}
}
先感谢您。