<small id='699ze'></small><noframes id='699ze'>

  • <legend id='699ze'><style id='699ze'><dir id='699ze'><q id='699ze'></q></dir></style></legend>
    <tfoot id='699ze'></tfoot>

        <i id='699ze'><tr id='699ze'><dt id='699ze'><q id='699ze'><span id='699ze'><b id='699ze'><form id='699ze'><ins id='699ze'></ins><ul id='699ze'></ul><sub id='699ze'></sub></form><legend id='699ze'></legend><bdo id='699ze'><pre id='699ze'><center id='699ze'></center></pre></bdo></b><th id='699ze'></th></span></q></dt></tr></i><div id='699ze'><tfoot id='699ze'></tfoot><dl id='699ze'><fieldset id='699ze'></fieldset></dl></div>
        • <bdo id='699ze'></bdo><ul id='699ze'></ul>

      1. Android 警报对话框无法找到视图

        时间:2023-05-28
              <tbody id='18AVu'></tbody>
              <bdo id='18AVu'></bdo><ul id='18AVu'></ul>
            • <legend id='18AVu'><style id='18AVu'><dir id='18AVu'><q id='18AVu'></q></dir></style></legend>

                <tfoot id='18AVu'></tfoot>

                • <small id='18AVu'></small><noframes id='18AVu'>

                • <i id='18AVu'><tr id='18AVu'><dt id='18AVu'><q id='18AVu'><span id='18AVu'><b id='18AVu'><form id='18AVu'><ins id='18AVu'></ins><ul id='18AVu'></ul><sub id='18AVu'></sub></form><legend id='18AVu'></legend><bdo id='18AVu'><pre id='18AVu'><center id='18AVu'></center></pre></bdo></b><th id='18AVu'></th></span></q></dt></tr></i><div id='18AVu'><tfoot id='18AVu'></tfoot><dl id='18AVu'><fieldset id='18AVu'></fieldset></dl></div>
                  本文介绍了Android 警报对话框无法找到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法让 AlertDialog 将文本传递回调用它的活动.问题似乎是调用 findViewByID 时找不到正确的 EditText,但我是 Android 新手,不知道为什么会这样.

                  I'm having trouble getting an AlertDialog to pass text back to the activity that calls it. It seems the issue is that it fails to find the proper EditText when calling findViewByID, but I'm new to Android and don't know why that may be.

                  代码如下:

                  public class ModifyDialogFragment extends DialogFragment {
                  
                  /* The activity that creates an instance of this dialog fragment must
                   * implement this interface in order to receive event callbacks.
                   * Each method passes the DialogFragment in case the host needs to query it. */
                  public interface MDialogListener {
                      public void onMDialogPositiveClick(String newValue);
                  }
                  
                  // Use this instance of the interface to deliver action events
                  MDialogListener mListener;
                  
                  String mEntryName = "";
                  EditText mEditText;
                  
                  @Override
                  public Dialog onCreateDialog(Bundle savedInstanceState) {
                      // Use the Builder class for convenient dialog construction
                      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                  
                      LayoutInflater inflater = getActivity().getLayoutInflater();
                  
                      final View modifyView = inflater.inflate(R.layout.modify_dialog, null);
                  
                      builder.setView(modifyView);
                  
                      final EditText editText = (EditText) getActivity().findViewById(R.id.modificationText);
                  
                  
                  
                             builder.setPositiveButton(R.string.modify, new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int id) {
                                     mListener.onMDialogPositiveClick(editText.getText().toString());
                                 }
                             });
                             builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int id) {
                                     // User cancelled the dialog
                                 }
                             });
                  
                      // Create the AlertDialog object and return it
                      return builder.create();
                  }
                  
                  // Override the Fragment.onAttach() method to instantiate the ModifyDeleteDialogListener
                  @Override
                  public void onAttach(Activity activity) {
                      super.onAttach(activity);
                      // Verify that the host activity implements the callback interface
                      try {
                          // Instantiate the MDDialogListener so we can send events to the host
                          mListener = (MDialogListener) activity;
                      } catch (ClassCastException e) {
                          // The activity doesn't implement the interface, throw exception
                          throw new ClassCastException(activity.toString()
                                  + " must implement MDialogListener");
                      }
                  }
                  

                  以及对应的 modify_dialog.xml:

                  And the corresponding modify_dialog.xml:

                  <?xml version="1.0" encoding="utf-8"?>
                  
                  <EditText xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                      android:id="@+id/modificationText"
                      android:inputType="text"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="16dp"
                      android:layout_marginLeft="4dp"
                      android:layout_marginRight="4dp"
                      android:layout_marginBottom="4dp"/>
                  

                  为什么没有找到editText?我可以做些什么来使这项工作按预期工作,将新字符串传递回活动?

                  Why isn't the editText being found? What can I do to make this work as intended, passing the new string back to the activity?

                  推荐答案

                  改变

                  final EditText editText = (EditText) getActivity().findViewById(R.id.modificationText);
                  

                  final EditText editText = (EditText) modifyView.findViewById(R.id.modificationText);
                  

                  您的 EditText 位于 modify_dialog.xml 中,因此您需要使用该 layout 膨胀的变量(此处为 modifyView) 来查找 id 而不是 getActivty() 将查找的 layout.

                  Your EditText lives in modify_dialog.xml so you need to use the variable that was inflated with that layout (here modifyView) to find the id not the layout that getActivty() will look in.

                  这篇关于Android 警报对话框无法找到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android:将 EditText 限制为数字 下一篇:Android - 在用户输入之前和之后检查editText是否> 3

                  相关文章

                    <i id='mW9uM'><tr id='mW9uM'><dt id='mW9uM'><q id='mW9uM'><span id='mW9uM'><b id='mW9uM'><form id='mW9uM'><ins id='mW9uM'></ins><ul id='mW9uM'></ul><sub id='mW9uM'></sub></form><legend id='mW9uM'></legend><bdo id='mW9uM'><pre id='mW9uM'><center id='mW9uM'></center></pre></bdo></b><th id='mW9uM'></th></span></q></dt></tr></i><div id='mW9uM'><tfoot id='mW9uM'></tfoot><dl id='mW9uM'><fieldset id='mW9uM'></fieldset></dl></div>
                      <bdo id='mW9uM'></bdo><ul id='mW9uM'></ul>

                  1. <legend id='mW9uM'><style id='mW9uM'><dir id='mW9uM'><q id='mW9uM'></q></dir></style></legend>

                    1. <small id='mW9uM'></small><noframes id='mW9uM'>

                      <tfoot id='mW9uM'></tfoot>