爬虫類嫌いのPython日記

爬虫類が大の苦手の筆者が、Pythonに挑戦。他にも、RubyやObjective-C、Google Appengine、Herokuなど色々とチャレンジしています。

AndroidでAlertDialogを表示する

OKボタンのみのダイアログを表示する

package com.example.dialogtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainTest extends Activity {
	private EditText mResultText = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button;
        button = (Button) findViewById(R.id.ButtonAlertDialogOK);
        button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			       AlertDialog alertDialog = new AlertDialog.Builder(DialogTest.this)
                               .setTitle("Alert Dialog OK")
                               .setMessage("Alert Dialog with OK Button only.")
                               .setPositiveButton("OK", new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {
						mResultText.setText("Alert Dialog [OK] pressed");
					}
				})
				.create();
				alertDialog.show();
			}
		});
		mResultText = (EditText)findViewById(R.id.TextResult);
    }
}

3つのボタンが並んだダイアログを表示する

package com.example.dialogtets;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DialogTest extends Activity {
	private EditText mResultText = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button;
        button = (Button) findViewById(R.id.ButtonAlertDialogOK);
        button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
                               AlertDialog alertDialog = new AlertDialog.Builder(DialogTest.this)
                               .setTitle("Alert Dialog OK")
                               .setMessage("Alert Dialog with OK Button only.")
                               .setPositiveButton("OK", new DialogInterface.OnClickListener(){
                                       public void onClick(DialogInterface dialog, int which) {
						mResultText.setText("Alert Dialog [OK] pressed");
					}
			       })
                               .setNegativeButton("Nagative", new DialogInterface.OnClickListener() {
				       @Override
                                       public void onClick(DialogInterface dialog, int which) {
					       mResultText.setText("Alert Dialog [Negative] pressed");
					}
			       })
                               .setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
				       @Override
				       public void onClick(DialogInterface dialog, int which) {
					       mResultText.setText("Alert Dialog [Neutral] pressed");
					}
			       })
			       .create();
			       alertDialog.show();
			}
		});
		mResultText = (EditText)findViewById(R.id.TextResult);
    }
}
ボタンの種類
  • setPositiveButton

OKボタンなどの肯定的な意味を持つボタンを設定します。
他のボタンと並べて表示した場合、ボタンの位置は左側になります。

  • setNegativeButton

Cancelボタンなどの否定的な意味を持つボタンを設定します。
他のボタンと並べて表示した場合、ボタンの位置は右側になります。

  • setNeutralButton

あまり使用しませんが、中立的な意味を持つボタンを設定します。
(どちらでもないなど)
他のボタンと並べて表示した場合、ボタンの位置は中央になります。

リスト表示型AlertDialog

package com.example.dialogtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	private EditText mResultText = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button;
        button = (Button) findViewById(R.id.ButtonAlertDialogOK);
        button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final String[] DIALOG_ITEM = new String[]{"aaa", "bbb", "ccc", "ddd"};
				AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
				.setTitle("Alert Dialog List")
				.setItems(DIALOG_ITEM, new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {
						mResultText.setText(DIALOG_ITEM[which]);
					}
				})
				.create();
				alertDialog.show();
			}
		});
		mResultText = (EditText)findViewById(R.id.TextResult);
    }
}
リスト要素の生成
final String DIALOG_ITEM = new String{"aaa", "bbb", "ccc", "ddd"};

リストの要素を配列で定義し、setItemsメソッドにセットするだけです。