如何在Android中制作警报对话框?

时间:2012-09-26 11:08:33

标签: android

我曾经读过一个教程,但从那以后我的手机被XBOX消灭了,所以我输了。我希望在MainActivity上有一个按钮,打开一个AlertDialog Box,说明应用信息,例如版本号,当前版本,这个版本的Android版本等等。

 package com.apw.games.rpg.medieval;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.*;
import android.util.*;
import android.graphics.*;

public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override public void onNothingSelected(AdapterView<?> parent) {

     }
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); return true; }

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 


        case R.id.quit: 
        Intent intent =  new Intent(this, Exit.class); 
        startActivity(intent); 
        return true; 
        case R.id.new_game: 
            Intent i = new Intent(this, New_Game.class); 
            startActivity(i); 
            return true; 
        case R.id.visit_site: 
            Intent inte = new Intent(this, Site.class); 
            startActivity(inte); 
            return true; 
        default: return super.onOptionsItemSelected(item);

        }}

4 个答案:

答案 0 :(得分:6)

首先声明AlertDialog类型对象:

AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create();

Main.this是我的活动的背景。 您可以像这样设置对话框的标题:

alertDialog.setTitle("Title");

并留言:

alertDialog.setMessage("Your text");

接下来,设置你的按钮功能:

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int which) {

   //here you can add functions

} });

您可以使用以下行更改AlertDialog的图标: alertDialog.setIcon(R.drawable.icon);

最后,不要忘记展示你的对话:

alertDialog.show();

答案 1 :(得分:2)

  AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this)
        .setTitle("alert dialog")
        .setMessage("message")

        .setPositiveButton("ok", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {                            
               Activity.this.finish();  

            }
        })

        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Activity.this.finish();
            }
        })
        .show();    

答案 2 :(得分:2)

 // here is a snippet code work for me   
    new AlertDialog.Builder(this)
    .setTitle("Mobile Raksha")
    .setMessage(
            "Your Message")
    .setCancelable(true)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            arg0.dismiss();
            finish();
        }
    }).show();

答案 3 :(得分:-1)

以上解释很好。基于类类型的安卓对话框有四种类型:AlertDialog,Progress Dialog,DatePickerDialog,TimePickerDialog。我们可以根据需要选择。如果显示消息的简单对话框,只需使用AlertDialog。如果您想逐步阅读创建对话框的过程,请在how to create alert dialog box in android上查看这个简单示例。

相关问题