警报对话框中的可单击文本

时间:2016-03-05 14:54:20

标签: android android-layout android-alertdialog clickable

我想在alert dialog中制作一个可点击的文字。实际上文本将是一个ID。因此,当您单击它时,它将打开浏览器并转到特定网页。我该怎么办? 他是我的代码:

alertDialog.Builder mBuilder = new AlertDialog.Builder(this);
mBuilder.setMessage("@sampleID");
mBuilder.setCancelable(true);
AlertDialog mAlert = mBuilder.create();
mAlert.show();    

另外,如何在警告对话框中添加多行?(每行不同的ID)

1 个答案:

答案 0 :(得分:1)

您应该创建一个包含所需内容的布局,并使用setContentView将布局添加到AlertDialog。

LayoutInflater inflater = LayoutInflater.from(this);
RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.alert_custom, null);
TextView tv1 = layout.findViewById(R.id.your_item_0);
tv1.setOnClickListener(new OnClickListener());
final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
dialog.setCancelable(true);
dialog.show();
dialog.getWindow().setContentView(layout);