android对话框 - 如何查找我的项目的ViewById

时间:2015-09-15 14:21:24

标签: android android-layout android-dialog

我的TextView可以获得此对话框的消息,如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("your message");
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);

但是,如何将这些字符串项添加到TextView ???

final CharSequence[] items = { "String 1", "String 2", "String 3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items,new DialogInterface.OnClickListener(....
AlertDialog dialog = builder.show();
TextView item = (TextView)dialog.findViewById(android.R.id.?);
请帮助我~~

1 个答案:

答案 0 :(得分:1)

这些是列表项,而不是TextView。您将在setItems方法的回调中获取所选项目

builder.setItems(items, new DialogInterface.OnClickListener()
    @Override
    public void onClick(DialogInterface dialog, int which) {
        String chosen = items[which].toString();
    }
});