このチュートリアルでは、Androidにアラートボックスを表示する方法を説明します。

流れるステップを参照してください:

  1. まず、AlertDialog.Builderを使用してアラートボックスを作成します

タイトル、表示するメッセージ、ボタン、ボタンonclickなどのインターフェイス
関数
。後でビルダーの上に「AlertDialog」を付けて表示します。

  1. 完了しました.


P.SこのプロジェクトはEclipse 3.7で開発され、Android 2.3.3.

でテストされています

1 Androidレイアウトファイル

Simpelレイアウトファイルは、画面上にボタンを表示します。

ファイル:res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout__width="fill__parent"
    android:layout__height="fill__parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonAlert"
        android:layout__width="wrap__content"
        android:layout__height="wrap__content"
        android:text="Show Alert Box"/>

</LinearLayout>

2.活動

ユーザーがこのボタンをクリックすると、あらかじめ定義されたアラートダイアログインターフェイスでアラートボックスが表示されます。


File:MainActivity.java

package com.mkyong.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
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 {

    final Context context = this;
    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonAlert);

       //add button listener
        button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

           //set title
            alertDialogBuilder.setTitle("Your Title");

           //set dialog message
            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                       //if this button is clicked, close
                       //current activity
                        MainActivity.this.finish();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                       //if this button is clicked, just close
                       //the dialog box and do nothing
                        dialog.cancel();
                    }
                });

               //create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

               //show it
                alertDialog.show();
            }
        });
    }
}

デモ

それを開始し、ボタンを表示します。


アンドロイドの警告ボックスの例、title = "アンドロイドの警告ボックスの例"、width = 318、height = 480

ボタンをクリックすると、アラートボックスが表示されます


android-alert-box-example-1

、width = 318、height = 480]

「はい」ボタンをクリックした場合は、アクティビティを閉じてAndroidのメイン画面に戻ります。


アンドロイドの警告ボックスの例、title = "android-alert-box-example-2"、width = 318、height = 480

ソースコードをダウンロードする

ダウンロードする –

Android-Alert-Dialogl-Example.zip

(16 KB)