首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android对话界面获取内部对话视图

Android对话界面获取内部对话视图
EN

Stack Overflow用户
提问于 2012-03-19 21:38:21
回答 6查看 47.1K关注 0票数 34

我有一个简单程序来显示一个对话框,其中有一个edittext视图,并监听肯定/否定按钮,以便在每个按钮中执行自定义操作(读取edittext并将其内容保存到活动变量)。

当我看不到从对话框界面恢复当前对话框的任何方法时,问题就出现了(然后,我就不能恢复对话框中的任何视图)。

这可能是个菜鸟问题,但在谷歌上搜索了几次后,我找不到一个人的答案。

我的代码如下

代码语言:javascript
复制
LayoutInflater li = LayoutInflater.from(this);
View myView = li.inflate(R.layout.my_layout, null);

AlertDialog.Builder cDialog = new AlertDialog.Builder(this);
cDialog.setView(myView);
cDialog.setPositiveButton(R.string.start_download, new   DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
    //here the code to retrieve dialog
  }
});
cDialog.create();

有没有线索告诉我在哪里可以找到它?

EN

回答 6

Stack Overflow用户

发布于 2012-03-19 21:41:03

您可以使用以下命令从onClick()中的Dialog检索视图:

代码语言:javascript
复制
EditText edit = (EditText) ((AlertDialog) dialog).findViewById(R.id.the_id_of_view);
票数 79
EN

Stack Overflow用户

发布于 2012-12-20 03:20:27

这种方式在我的代码中运行得很好:

代码语言:javascript
复制
public void onClick(DialogInterface dialog, int which) {

   Dialog dialog2 =Dialog.class.cast(dialog);
   EditText edit = (EditText) dialog2.findViewById(R.id.myedittext);

}

干杯

票数 14
EN

Stack Overflow用户

发布于 2015-03-22 18:24:36

您已经拥有对包含编辑文本的视图的引用。为什么不直接使用它呢?只要确保视图是最终的,这样就可以在OnClickListener匿名类中访问它。

代码语言:javascript
复制
LayoutInflater li = LayoutInflater.from(this);
final View myView = li.inflate(R.layout.my_layout, null);
// don't forget to mark myView as final

AlertDialog.Builder cDialog = new AlertDialog.Builder(this);
cDialog.setView(myView);
cDialog.setPositiveButton(R.string.start_download, new   DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
    //here the code to retrieve dialog
    EditText edit = (EditText) myView.findViewById(R.id.the_id_of_view);
  }
});
cDialog.create();
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9771228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档