Clear text in EditText when entered

后端 未结 18 1466
醉话见心
醉话见心 2020-11-27 03:44

I\'m trying to set and onclicklistener so that when I click within the edittext element it will clear its current contents. Is there something wrong here? When I compile thi

相关标签:
18条回答
  • 2020-11-27 04:30

    If the use of EditText is not mandatory, you can implement this behavior easily with the new material components:

    <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_field"
                app:endIconDrawable="@drawable/ic_close_black_24dp"
                app:endIconMode="clear_text"
                app:endIconTint="@color/black">
    
                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_value"
                    android:maxLines="1"
                    android:text="@{itemModel.value}" />
    
            </com.google.android.material.textfield.TextInputLayout>
    

    You only have to specify the drawable you want for the button that will clear the text and the action that it will execute. To clear the text, you can use iconMode="clear_text", but also "password_toggle" is available.

    0 讨论(0)
  • 2020-11-27 04:30

    I am not sure if your searching for this one

        {
        <EditText
         .
         . 
         android:hint="Please enter your name here">
        }
    
    0 讨论(0)
  • 2020-11-27 04:38

    by setting Empty string you can clear your edittext

        editext.setText("");
    
    0 讨论(0)
  • 2020-11-27 04:39
    final EditText childItem = (EditText) convertView.findViewById(R.id.child_item);
    childItem.setHint(cellData);
    
    childItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            //Log.d("NNN", "Has focus " + hasFocus);
            if (hasFocus) {
                Toast.makeText(ctx.getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(ctx.getApplicationContext(),
                "loss the focus", Toast.LENGTH_SHORT).show();
            }
            return;
        });
    
    0 讨论(0)
  • 2020-11-27 04:40

    just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box.

    0 讨论(0)
  • 2020-11-27 04:43

    We can clear EditText data in two ways

    First One setting EditText is empty like below line

    editext.setText("");
    

    Second one clearing EditText data like this

    editText.getText().clear();
    

    I suggest second way

    0 讨论(0)
提交回复
热议问题