【问题标题】:How to set not open editText when click on in android [duplicate]在android中单击时如何设置不打开editText [重复]
【发布时间】:2017-05-04 21:54:14
【问题描述】:

我想在点击Edittext 时显示我的对话框。但是在我的代码中,当点击EditText 时,首先选择editText 并显示keyboard 所以点击第二次点击editText 然后显示我的Dialog

我希望在点击editText 时不选择edittext 并且不显示键盘只显示对话框

我的 xml 代码:

    <android.support.design.widget.TextInputLayout
        android:id="@+id/registerCountryInptLay"
        style="@style/registerIptLytStyle"
        app:hintAnimationEnabled="true">

        <EditText
            android:id="@+id/registerCountryEdtTxt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/loginCountry"
            android:inputType="text"
            android:paddingLeft="@dimen/padding10" />
    </android.support.design.widget.TextInputLayout>

我为editText点击事件写了下面的代码:

@OnClick({R.id.registerCountryEdtTxt, R.id.registerCountryInptLay})
void selectCountry() {

    final CountryPicker picker = CountryPicker.newInstance("Select Country");  // dialog title
    picker.setListener(new CountryPickerListener() {
        @Override
        public void onSelectCountry(String name, String code, String dialCode, int flagDrawableResID) {
            // Implement your code here
            countryName = name;
            if (!countryName.isEmpty() && countryName != null) {
                countryList.setText(countryName);
            }
            picker.dismiss();
        }
    });
    picker.show(getSupportFragmentManager(), "COUNTRY_PICKER");
}

我该怎么办?

【问题讨论】:

    标签: android android-edittext android-alertdialog


    【解决方案1】:

    只需在 EditText 中添加以下属性:

    android:focusable="false"
    android:focusableInTouchMode="false"
    android:clickable="true"
    

    所以您的代码如下所示:

        <EditText
            android:id="@+id/registerCountryEdtTxt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/loginCountry"
            android:inputType="text"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="true"
            android:paddingLeft="@dimen/padding10" />
    </android.support.design.widget.TextInputLayout>
    

    【讨论】:

      【解决方案2】:

      您可以尝试在 OnClick 方法中添加它

      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                      imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
      

      【讨论】:

      • 感谢我的朋友,但不初始化此代码:view.getWindowToken()。我想在活动中使用此代码。
      • 这是在活动中强制隐藏软键盘。 for view (View view = getCurrentFocus();)
      【解决方案3】:

      检查此代码。我认为您正在使用 ButterKnife。

       registerCountryEdtTxt= (EditText) findViewById(R.id.registerCountryEdtTxt);
      
      
          registerCountryEdtTxt.setClickable(true);
          registerCountryEdtTxt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  void selectCountry()
              }
          });
      
      
          void selectCountry() {
      
              final CountryPicker picker = CountryPicker.newInstance("Select Country");  // dialog title
              picker.setListener(new CountryPickerListener() {
                  @Override
                  public void onSelectCountry(String name, String code, String dialCode, int flagDrawableResID) {
                      // Implement your code here
                      countryName = name;
                      if (!countryName.isEmpty() && countryName != null) {
                          countryList.setText(countryName);
                      }
                      picker.dismiss();
                  }
              });
              picker.show(getSupportFragmentManager(), "COUNTRY_PICKER");
          }
      

      【讨论】:

        【解决方案4】:

        如果你使用黄油刀意味着检查这个

         <android.support.design.widget.TextInputLayout
                android:id="@+id/registerCountryInptLay"
                style="@style/registerIptLytStyle"
                app:hintAnimationEnabled="true">
        
                <EditText
                    android:id="@+id/registerCountryEdtTxt"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/loginCountry"
                    android:inputType="text"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:clickable="true"
                    android:paddingLeft="@dimen/padding10" />
            </android.support.design.widget.TextInputLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-25
          • 1970-01-01
          • 2016-06-14
          相关资源
          最近更新 更多