EditTexts inside ExpandableListView

2019-05-06 16:55发布

问题:

Hi I'm trying to make an activity that displays an ExpandableListView as shown in the figure.

When you click on the group it expands and shows a LinearLayout with a textview and an editext.

Right know the problem I have is that when a click on the EditText it doesn't get focus and I have to tap twice on it in order to make it get focus and display the keyboard, and then when I enter text I see nothing initially while I'm typing and then when I hide the Keyboard the EditText is updated with the text I typed

I've tried adding android:descendantFocusability="afterDescendants" to the expandableListVie layout, but the behaviour is the same.

Any Idea?? or any other way I could implement this layout??

回答1:

add this to the manifest right after the android:name in your activity

 android:windowSoftInputMode="adjustPan"


回答2:

I am not sure how to fix the focusing problem, but I know why you have to collapse and expand the group to see the text in the editText. The listview doesn't refresh unless you scroll or you do something like expanding and collapsing the listview. So the best way I can think of to fix this problem is to create a list of strings that will be the values of the editTexts in the listview. Then create a onKeyClick() listener for each of the editTexts and do this:

editText1.setOnKeyListener(new EditText.OnKeyListener()
{
    public boolean onKey(View editText, int keyCode, KeyEvent keyEvent)
    {
        // Get the position of the item somehow
        int position = 4;
        // Get value from List<String> for editTexts
        String textValue = listOfValues.get(position);
        textValue = ((EditText)editText).getValue();
        adapter.notifyDataSetChanged();
        return false;
    }
});

What happens here is that you are getting the value of the editText once a key has been pressed, and are changing the value of the string in the list to the new value. Then you are telling the adapter that the value of one of the items has changed an it needs to refresh. Although this is not an efficient way to fix the problem, it is a backup solution in case you can't think of a better way.



回答3:

Try to store the values in the Edittext to a String variable using the function

String temp;

editText.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }
    public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
        // TODO Auto-generated method stub

    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

        temp = s.toString();//editText.getText().toString();

        childValues[groupPosition][childPosition][]= temp ;

        Toast.makeText(context, ""+childValues[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
    }
});

and in the getChild() function always check for the data in temp and settext() value to that particular edittext.



回答4:

You have to add two things.

1) expandableListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

2)In manifest(on your activity) android:windowSoftInputMode="adjustPan"