Clear text in EditText when entered

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

    i don't know what mistakes i did while implementing the above solutions, bt they were unsuccessful for me

    txtDeck.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override       
        public void onFocusChange(View v, boolean hasFocus) {
            txtDeck.setText("");
        }
    });
    

    This works for me,

    0 讨论(0)
  • 2020-11-27 04:26
    public EditText editField;
    public Button clear = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
        setContentView(R.layout.text_layout);
       this. editField = (EditText)findViewById(R.id.userName);
    this.clear = (Button) findViewById(R.id.clear_button);  
    this.editField.setOnClickListener(this);
    this.clear.setOnClickListener(this);
    @Override
    public void onClick(View v) {
    
        // TODO Auto-generated method stub
    if(v.getId()==R.id.clear_button){
    //setText will remove all text that is written by someone
        editField.setText("");
        }
    }
    
    0 讨论(0)
  • 2020-11-27 04:27

    Also you can use code below

    editText.getText().clear();
    
    0 讨论(0)
  • 2020-11-27 04:28

    Your code should be:

        public class Project extends Activity implements OnClickListener {
                /** Called when the activity is first created. */
                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) {
                    // TODO Auto-generated method stub
    
                    if(v == editText) {
                        editText.setText("");
                    }
                }
            }
    
    0 讨论(0)
  • 2020-11-27 04:29
    package com.example.sampleproject;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    
    public class SampleProject extends Activity {
        EditText mSearchpeople;
        Button mCancel , msearchclose;
        ImageView mprofile, mContact, mcalender, mConnection, mGroup , mFollowup , msetting , mAddacard;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dashboard);
            mSearchpeople = (EditText)findViewById(R.id.editText1);
            mCancel = (Button)findViewById(R.id.button2);
            msearchclose = (Button)findViewById(R.id.button1);
            mprofile = (ImageView)findViewById(R.id.imageView1);
            mContact = (ImageView)findViewById(R.id.imageView2);
            mcalender = (ImageView)findViewById(R.id.imageView3);
            mConnection = (ImageView)findViewById(R.id.imageView4);
            mGroup = (ImageView)findViewById(R.id.imageView5);
            mFollowup = (ImageView)findViewById(R.id.imageView6);
            msetting = (ImageView)findViewById(R.id.imageView7);
            mAddacard = (ImageView)findViewById(R.id.imageView8);
        }
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            mCancel.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mSearchpeople.clearFocus();
    
                }
            });
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 04:30

    //To clear When Clear Button is Clicked

    firstName = (EditText) findViewById(R.id.firstName);

        clear = (Button) findViewById(R.id.clearsearchSubmit);
    
        clear.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (v.getId() == R.id.clearsearchSubmit);
                firstName.setText("");
            }
        });
    

    This will help to clear the wrong keywords that you have typed in so instead of pressing backspace again and again you can simply click the button to clear everything.It Worked For me. Hope It Helps

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