简体   繁体   中英

Make one EditText row clickable and not editable Android

I'm showing a list of items on the activity and I'm using an ArrayAdapter so I can only have a TextView/EditText in my xml. But I want to be able to edit the text and save that somehow. Right now... I can only edit but since I have no buttons, I can't save the content. Can I make one row from the list to be uneditable but clickable? To simulate the action of a button?

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_edit_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#035E7B"
    />

Something like :

Row1Editable

Row2Editable

Row3Editable

ClickHere to save your edits

    public class SingleListItem extends ListActivity {
    SweetsXmlController controller;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_list_item);
        controller = new SweetsXmlController();

        Intent intent = getIntent();
        int position = intent.getIntExtra("position", 1);
        String[] mTestArray = getResources().getStringArray(R.array.sections);
        Sweet toShow = controller.getSweetByPosition(position, mTestArray);

        ArrayList<String> res = new ArrayList<>();
        res.add(toShow.getName());
        res.add("$" + toShow.getPrice());
        res.add(toShow.getDescription());
        res.add(toShow.getCalories() + "kcal");res.add("Click here to save your changes!");

        EditText editText = (EditText) findViewById(R.id.my_edit_text);
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.d("HAAAAAAAAAAAAH", "Trying to MOVE TO NEXT ITEM ");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        ArrayAdapter<String> adapter;
        adapter = new ArrayAdapter<String>(
                this,
                R.layout.activity_single_list_item,
                res);
        setListAdapter(adapter);



    }

You will need to write a custom adapter for this - have a look at http://www.vogella.com/tutorials/AndroidListView/article.html . In the adapter's getView() method, you can use the EditText.setEnabled(boolean) method to toggle whether it is editable or not.

Also, if all you need is a single button, it would be easier to have a separate listview + a button in your layout.

I think you can use this method below:

EditText myEditTextField = new EditText(this);

myEditTextField.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

After text changed, just save the output in textView.

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