我已經在android.now中做了一個簡單的警告框的程序,我必須把兩個按鈕「確定」和「取消」,但是當我運行該程序時,它只顯示「取消「按鈕...我的代碼如下:如何把兩個按鈕放在警戒框
Main.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
}
});
}
}
預先感謝您。
請仔細閱讀此代碼:http://www.mkyong.com/android/android-alert-dialog-example/ – krishna