【问题标题】:Android - Modifying the handle to something in a Dialog [duplicate]Android - 将句柄修改为对话框中的某些内容[重复]
【发布时间】:2011-12-25 21:22:58
【问题描述】:

可能重复:
Android - Getting the handle to components in a Custom Dialog

我之前问过一个类似的问题。但是,当我尝试将句柄修改为 customDialog 中定义的元素之一时,我的代码仍然崩溃

    super.onCreate(savedInstanceState);
    setContentView(R.layout.customdialog);

    Context mContext = getApplicationContext();
    Dialog dialog = new Dialog(mContext);

    dialog.setContentView(R.layout.customdialog);
    dialog.setTitle("Send Message");
    EditText phoneNumber = (EditText)dialog.findViewById(R.id.customDialogPhoneNumber1);
    phoneNumber.setText("Hello");

如果我只是删除 phoneNumber.setText("...") ,代码将停止崩溃并显示对话框。但是,我确实需要从各种元素的句柄中获取东西。

知道我做错了什么吗?

【问题讨论】:

  • 相反,您应该在上一个问题中提到/更新相同的内容。别着急,肯定会有人回答的。在这里问多余的问题并不可取。
  • 对不起 Paresh - 我会确保我不会再这样做了。我对stackoverflow很陌生..
  • 请不要对我说对不起,我只是让你了解这个社区。您应该先阅读此常见问题解答:stackoverflow.com/faq#dontask,不要担心它会发生,因为您是 SO 新手。

标签: android android-layout android-emulator android-widget


【解决方案1】:

试试这个:

创建一个扩展对话框的类,并在其 onCreate 中执行 setContentView 和按钮的实例化等。 示例:

public CustomDialog(Context context) {
  super(context);
}

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  b = (Button) findViewById(R.id.button_left);
}

public void setBListener(View.OnClickListener bListener) {
  b.setOnClickListener(bListener);
}

在你的活动中:

CustomDialog d = construct your custom dialog
OnClickListener onBClick = construct your onclicklistener
d.setBListener(onBClick);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    相关资源
    最近更新 更多