Clear text in EditText when entered

后端 未结 18 1485
醉话见心
醉话见心 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:44

    It's simple: declare the widget variables (editText, textView, button etc.) in class but initialize it in onCreate after setContentView.

    The problem is when you try to access a widget of a layout first you have to declare the layout. Declaring the layout is setContentView. And when you initialize the widget variable via findViewById you are accessing the id of the widget in the main layout in the setContentView.

    I hope you get it!

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

    For Kotlin:

    Create two extensions, one for EditText and one for TextView

    EditText:

    fun EditText.clear() { text.clear() }
    

    TextView:

    fun TextView.clear() { text = "" }
    

    and use it like

    myEditText.clear()
    
    myTextView.clear()
    
    0 讨论(0)
  • 2020-11-27 04:46

    In XML you can write like:

        <EditText
            android:id="@+id/txtsearch"
            android:layout_width="250dp"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:background="@drawable/roundlayoutbutton1"
            android:ems="10"
            android:gravity="center"
            android:inputType="text"
            android:textAllCaps="true"
            android:text="search_xxxx"
    
            android:textColor="@color/colorPrimaryDark"
            android:visibility="visible" />
    

    and in java class you may have below one :

        EditText searchHost;
    

    OnCreate() you write:

        searchHost=findViewById(R.id.txtsearch);
    
        searchHost.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(searchHost.getText().toString().equalsIgnoreCase("search_xxxx")){
                    searchHost.setText("");
                    Toast.makeText(getApplicationContext(),"Enter you text xxx...", Toast.LENGTH_LONG).show();
                }
            }
        });
    

    It works fine for me.

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

    Very Simple to clear editText values.when u click button then only follow 1 line code.

    Inside button or anywhere u want.Only use this

    editText.setText("");   
    
    0 讨论(0)
  • 2020-11-27 04:47

    You can use the 'android:hint' attribute in your EditText also from code:

    editText.setHint(CharSequence hint / int resid);
    

    Then you don't need any onClickListener or similar. But consider that the hint value won't be passed. The editText will be stayed empty. In this case you can set your editText with your deflault value:

    if(editText.getText().toString().equals("")) { 
    ...use your default value instead of the editText... }
    
    0 讨论(0)
  • 2020-11-27 04:49

    First you need to call setContentView(R.layout.main) then all other initialization.

    Please try below Code.

    public class Trackfolio extends Activity implements OnClickListener {
        /** Called when the activity is first created. */
        public EditText editText;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
    
            editText = (EditText) findViewById(R.id.editText1);
            editText.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            editText.getText().clear(); //or you can use editText.setText("");
        }
    }
    
    0 讨论(0)
提交回复
热议问题