【问题标题】:Android EditText with icon/button带有图标/按钮的 Android EditText
【发布时间】:2016-05-19 06:11:12
【问题描述】:

我怎样才能获得下面的EditText

x 按钮删除插入的文本,并且只要按下眼睛按钮,它就会显示清除密码。请注意,我将它们称为按钮,因为我真的不知道它们实际上是什么,也不知道它们是否是 EditText 本身的一部分,或者它们是否是独立视图。

【问题讨论】:

  • 你可以在editText中使用drawableRight。搜索它,你会发现很多关于它的信息。
  • @androidnoobdev 谢谢你的信息;我会查一下

标签: android android-edittext


【解决方案1】:

您可以在 EditText 的右侧添加一个按钮

<FrameLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ScreenLoginContainer">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edit_text_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:imeOptions="actionDone"
            android:hint="@string/enter_password_hint"
            android:paddingRight="30dp"
            style="@style/ScreenPasswordEditText"/>

    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+screen_card_ean_enter/show_password_button"
        style="@style/ShowHidePasswordButton"/>

</FrameLayout>

【讨论】:

  • 这正是我想要的,虽然我用 ImageView 替换了 button
【解决方案2】:

你可以使用android:drawableRight

EditText 中,在 XML 中,在文本右侧设置一个 可绘制对象

android:drawableRight="@drawable/Your_drawable"

【讨论】:

【解决方案3】:

设计支持库有一个setPasswordVisibilityToggleEnabled方法:https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setPasswordVisibilityToggleEnabled(boolean)

这对你的密码编辑文本有好处,但不是用户名。

【讨论】:

    【解决方案4】:

    你可以使用这个: android:drawableRight="@drawable/your_icon" 也可以点击这个answer

    可能有帮助

    【讨论】:

      【解决方案5】:

      这是在末尾创建带有十字按钮的EditText 的完美方法:

      <FrameLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="9dp"
      android:padding="5dp">
      
      <EditText
          android:id="@+id/calc_txt_Prise"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:inputType="numberDecimal"  
          android:layout_marginTop="20dp"
          android:textSize="25dp"
          android:textColor="@color/gray"
          android:textStyle="bold"
          android:hint="@string/calc_txt_Prise"
          android:singleLine="true" />
      
      <Button
          android:id="@+id/calc_clear_txt_Prise"      
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginRight="10dp"
          android:layout_gravity="right|center_vertical"
          android:background="@drawable/delete" />
      

      现在要在单击时清除 EditText,您可以使用

      button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  EditTextID.setText("");
              }
          });
      

      为了使密码在按下按钮之前可见,您可以这样实现:

      yourButton.setOnTouchListener(new OnTouchListener() {
              public boolean onTouch(View v, MotionEvent event) {
      
                     switch ( event.getAction() ) {
                      case MotionEvent.ACTION_DOWN: 
                         editText.setInputType(InputType.TYPE_CLASS_TEXT);
                      break;
                      case MotionEvent.ACTION_UP: 
                          editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                      break;
                      }
                      return true;
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-11
        • 2014-05-22
        • 2022-01-11
        • 2012-07-07
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多