Created
January 21, 2015 07:43
-
-
Save wasnot/53f3a5555d12a90534f8 to your computer and use it in GitHub Desktop.
AlertDialogをボタンタップ等で閉じない ref: http://qiita.com/wasnot/items/3dd9d4cf1754562fa4bd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AlertDialog.Builder b = new AlertDialog.Builder(activity); | |
b.setTitle("タイトル"); | |
b.setPositiveButton(android.R.string.ok, null); | |
// ここでリスナーを実装しても渡されないので実装しなくても構いません。 | |
b.setNegativeButton(android.R.string.cancel, null); | |
// AlertDialogを生成する前にgetButton等してもnullが返されます。 | |
final AlertDialog dialog = b.show(); | |
// 種類に応じてButtonを取得します | |
Button button = dialog.getButton(DialogInterface.BUTTON_NEGATIVE); | |
// dialog.getButton(DialogInterface.BUTTON_POSITIVE); | |
// dialog.getButton(DialogInterface.BUTTON_NEUTRAL); | |
// 通常のViewのように実装します。 | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
... | |
// 場合によっては自分で明示的に閉じる必要がある | |
dialog.dismiss(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AlertDialog.Builder b = new AlertDialog.Builder(activity); | |
b.setTitle("タイトル"); | |
// ここでリスナーを実装しても渡されないので実装しなくても構いません。 | |
b.setItems(new String[]{"1","2"}, null); | |
// AlertDialogを生成する前にgetListView等してもnullが返されます。 | |
AlertDialog dialog = b.show(); | |
// ListViewを取得します | |
ListView list = dialog.getListView(); | |
// 通常のListViewのように実装します。 | |
button.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
... | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment