final CharSequence[] items = {"Now", "Later", "Cancel"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("When you need to Take BackUp?"); builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { // Toast.makeText(getApplicationContext(),items[item],Toast.LENGTH_SHORT).show(); items[0].notify(); items[1].charAt(2); } }); builder.show(); 此对话框可以完成一些功能,怎样声明和定义对话框对象呢?
2 回答
桃花长相依
TA贡献1860条经验 获得超8个赞
被选中的item会被传递到onClick方法中,item是onClick方法的一个参数。public void onClick(DialogInterface dialog, int item) { switch(item) { case 0: // the first item in the CharSequence[] callUserClickedNow(); break; case 1: // the second item in the CharSequence[] callUserClickedLater(); break; case 2: // the thirditem in the CharSequence[] callUserClickedCancel(); break; } dialog.dismiss(); } 然后,在方法外就可以创建AlertDialog对象了:private void callUserClickedNow() { // do stuff }
添加回答
举报
0/150
提交
取消