简体   繁体   中英

How to make clickable and editable Textfield in Android?

I am working on Android application in which I want to make my textfield editable and clickable. It has multiple TextFields and EditTexts on my screen. I have "EDIT" TextField for which I want to make it clickable and after clicking I want to make other field editable and enable. Without clicking edit Textfield all of them should not be enable.

My code snippet is given below:

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);

    fName = (TextView) findViewById(R.id.fnametxt);
    lName = (TextView) findViewById(R.id.lastnameTxt);
    mailText = (TextView) findViewById(R.id.mailTxt);
    mobileText = (TextView) findViewById(R.id.mobileTxt);
    dobText = (TextView) findViewById(R.id.dobTxt);

    fName.setText(currentUserFirstName);
    lName.setText(currentUserLastName);
    dobText.setText("");
    mobileText.setText(currentUserContactNumber);
    mailText.setText(currentUserEmail);
}

You can't convert TextView to EditText, but you can rather use setEnabled property for EditText

You can use editText.setEnabled(true); to make the EditText editable.

Say you are having two edittexts as follows. And on entering data in first edittext, you need to make edittext2 editable, then you can do this:

EditText edittext1, edittext2;
//findViewByIds for both views

editText1.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start,
        int before, int count) {
        if(s.length() != 0)
            editText2.setEnabled(true);
        else
            editText2.setEnabled(false);
    }
});

Hope this gives a clue how to use it.

EDIT:

Don't get confused between TextField, Button, EditText.

In Android, simple read only field is TextView. Editable textbox is called EditText, and Button is plain Button.

So as per what you are saying, you want tomake EditTexts editable upon clicking of a Button.

Use this:

EditText edittext1, edittext2;
Button button;
//findViewByIds for all views.
buton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        editText1.setEnabled(true);
        editText2.setEnabled(true);
    }
});

In my opinion, if you want to edit your textfield, then its better to go with EditText. The reason is as follows

The TextView's editable param does make it editable (with some restrictions).

If you set

android:editable="true"

you can access the TextView via the D-pad, or you could add

android:focusableInTouchMode="true" 

to be able to gain focus on touch.

The problem is you cannot modify the existing text, and you cannot move the cursor. The text you write just gets added before the existing text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM