关于自定义对话框
我应该列出可点击的项目。我暂时写了文字。我如何使它们可点击?抱歉,我刚刚开始在 Android 中编程。你能帮我看一下代码吗?
public class CustomDialog extends Dialog
{
public CustomDialog(Context context)
{
super(context, android.R.style.Theme_Translucent_NoTitleBar);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.privacy_popup);
TextView text = ((TextView)this.findViewById(R.id.text));
text.setText(Html.fromHtml("<b>item<b>"));
//text.setText("Privacy");
TextView text2 = (TextView) findViewById(R.id.text2);
text2.setText(Html.fromHtml("<b>item2<b>"));
TextView text3 = (TextView) findViewById(R.id.text3);
text3.setText(Html.fromHtml("<b>item3<b>"));
TextView text4 = (TextView) findViewById(R.id.text4);
text4.setText(Html.fromHtml("<b>item4<b>"));
}
}
I should make a list of clickable items. I wrote text for now. How do I make them clickable? Sorry but I just started to program in Android. Can you help me with the code?
public class CustomDialog extends Dialog
{
public CustomDialog(Context context)
{
super(context, android.R.style.Theme_Translucent_NoTitleBar);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.privacy_popup);
TextView text = ((TextView)this.findViewById(R.id.text));
text.setText(Html.fromHtml("<b>item<b>"));
//text.setText("Privacy");
TextView text2 = (TextView) findViewById(R.id.text2);
text2.setText(Html.fromHtml("<b>item2<b>"));
TextView text3 = (TextView) findViewById(R.id.text3);
text3.setText(Html.fromHtml("<b>item3<b>"));
TextView text4 = (TextView) findViewById(R.id.text4);
text4.setText(Html.fromHtml("<b>item4<b>"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,首先我建议您使用 DialogFragment 如果可能的话,但无论如何它是扩展对话框真的很容易,您可能需要检查 自定义对话框 来自官方安卓指南。
Well firstable i would suggest you using DialogFragment if it possible, but anyway it's really easy to extends a Dialog, you might want to check Custom Dialog from the official Android guide.
如果您被迫使用 TextView 元素,您可以实现 TextView
onClickListener
侦听器,如下所示:If you are forced to use TextView elements, you can implement the TextView
onClickListener
listener as this:您甚至不必扩展 Dialog 并创建自定义对话框;
AlertDialog
提供您正在寻找的所有功能。下面只不过是对话框主题的复制粘贴在开发者网站上。这将导致:
交换上面的
builder.setItems(...) for:
为每个选项添加单选按钮,或者:
用于复选框。
You don't even have to extend
Dialog
and create a custom dialog; anAlertDialog
provides all the functionality you're looking for. Below is nothing more than a copy paste from the Dialog topic on the developer website.This will result in:
Swap out above
builder.setItems(...)
for:to add a radio button with each option, or:
for checkboxes.