そのコードの何が問題なのかわかりません。エミュレーターは機能しますが、ボタンはリスナーで機能しません。プロジェクトをクリーンアップし、リスナーを作成したところ、機能するはずでした。
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
public class MainActivity extends Activity {
CharSequence[] items = { "Goolge", "Apple", "MaC Os" };
boolean[] checkedItems = new boolean[items.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button btn= (Button)findViewById(R.id.dialog);
// btn.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
showDialog(0);
}
protected Dialog onCreatDialog(int id) {
switch (id) {
case 0 :
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("this is a List of items..");
builder.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getBaseContext(), "Ok clicked",
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "cancel cliked",
Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(
getBaseContext(),
items[which]
+ (isChecked ? " checked"
: " unchecked"),
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return null;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/click_me" />
</RelativeLayout>