Skip to content

Instantly share code, notes, and snippets.

@Taishi-Y
Last active December 18, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Taishi-Y/5d8a8b745de6629448ed to your computer and use it in GitHub Desktop.
Save Taishi-Y/5d8a8b745de6629448ed to your computer and use it in GitHub Desktop.
Dialogを使って複数項目から一つの項目を選択する(確認ボタンつき) ref: http://qiita.com/Taishi-Y/items/34975da9d1cfd0f72718
public class PostActivity extends AppCompatActivity{
private Button buttonPrefecture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_activity);
buttonPrefecture = (Button)findViewById(R.id.button_prefecture);
buttonPrefecture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choosePrefecture();
}
});
}
public void choosePrefecture(){
final String[] items = {"item_0", "item_1", "item_2"};
int defaultItem = 0; // デフォルトでチェックされているアイテム
new AlertDialog.Builder((this))
.setTitle("Selector")
.setSingleChoiceItems(items, defaultItem, null)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// item_which selected
}
})
.setNegativeButton("Cancel", null)
.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment