安卓飞机大战(三) 弹出对话框
在游戲時,不管是退出游戲還是選擇戰機,都要彈出一個對話框,需要以下代碼
按一個按鈕彈出對話框
Layout文件:(添加一個按鈕)
<Button
??????? android:id="@+id/button1"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="準備起飛" />
在MainActivity 中:
public class choiceActivity extends Activity {
?? ?private Button button1;
?? ?protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.choice);
??????? button1=(ImageButton)findViewById(R.id.p_w_picpathButton1);
??????? button2=(ImageButton)findViewById(R.id.p_w_picpathButton2);
??????? button1.setOnClickListener(new View.OnClickListener() {
?? ??? ??? ?
?? ??? ??? ?@Override
?? ??? ??? ?public void onClick(View arg0) {
?? ??? ??? ??? ?AlertDialog.Builder builder = new AlertDialog.Builder(choiceActivity.this);
??????????????? //??? 設置Title的圖標
??????????????? builder.setIcon(R.drawable.ic_yxlm);
??????????????? //??? 設置Title的內容
??????????????? builder.setTitle("進入游戲");
??????????????? //??? 設置Content來顯示一個信息
??????????????? builder.setMessage("確定選擇進入游戲嗎?");
??????????????? //??? 設置一個PositiveButton
??????????????? builder.setPositiveButton("確定", new DialogInterface.OnClickListener()
??????????????? {
??????????????????? @Override
??????????????????? public void onClick(DialogInterface dialog, int which)
??????????????????? {
??????????????????????? Toast.makeText(choiceActivity.this, "進入游戲 ", Toast.LENGTH_SHORT).show();
??????????????????????? Intent intent=new Intent(choiceActivity.this,userActivity.class);
?????? ??? ??? ??? ??? ?startActivity(intent);//跳轉到下一個界面userActivity
??????????????????????? finish();
??????????????????? }
??????????????? });
??????????????? //??? 設置一個NegativeButton
??????????????? builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
??????????????? {
??????????????????? @Override
??????????????????? public void onClick(DialogInterface dialog, int which)
??????????????????? {
??????????????????????? Toast.makeText(choiceActivity.this, "繼續選擇", Toast.LENGTH_SHORT).show();
??????????????????? }
??????????????? });
??????????????? builder.show();
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ?});
}
這樣就可以實現一個對話框了!
轉載于:https://blog.51cto.com/bigcrab/1685032
總結
以上是生活随笔為你收集整理的安卓飞机大战(三) 弹出对话框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两种计算和输出n内5要么9除尽互惠
- 下一篇: oc 调用c语言方法和oc的方法调用