Issues focusing EditTexts in a ListView (Android)

前端 未结 2 1468
独厮守ぢ
独厮守ぢ 2020-12-18 04:52

I have a ListView with a few EditTexts in each item. I have no issues with a hardware keyboard, but things freak out a bit with the soft keyboard. I have two issues.

<
相关标签:
2条回答
  • 2020-12-18 05:24

    You are facing ListView recycling issue. When you scroll up or down or when keyboard appears ListView again refreshes and lost your EditText's focus. So, first of all read this answer to understand ListView Recycling mechanism and then follow the suggestion from my side the solution of your current scenario in my mind.

    I suggest you should use a Button on ListView Item and text of this button should be generic like Enter Student Info or what ever you'd like. After clicking on this Button open AlertDialog and set your xml view (currently your all edittexts like et_gpa, et_min, et_max etc on listview items)

    For Example:

    btnInfo.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
    
                    showStudentInfoAlert();
    
    }
    
    });
    
    public void showStudentInfoAlert() 
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
    
           LayoutInflater in = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    View v = in.inflate(R.layout.your_xml_having_edittexts, null);
    
    EditText et_gpa = (EditText) v.findViewById(R.id.et_gpa);
    
    //add all edit texts like this and after that just set view on alert dialog
    
            builder.setTitle("Enter student's info");
    
            builder.setView(v);
    
            builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                   //save all edittexts values and do what erver you want with those values 
    
                }
            });
    
            dialog.show();
        }
    
    0 讨论(0)
  • 2020-12-18 05:34

    first of all when the screen loads,, focus is on the "Gr High School Scale" field, because of this <requestFocus /> in your xml. but this does no gurantee that the keyboard might show.. to make the keyboard to show use getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); also i am thinking that it looses focus immediately because the listview has taken focus away from the edittext

    <ListView
     android:id="@+id/listview_scale"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_above="@id/linlay_scale_save_buttons"
     android:layout_alignParentLeft="true"
     android:layout_below="@id/radioGroup1"
     android:focusable="true" //this
     android:focusableInTouchMode="true" // and that
     android:headerDividersEnabled="true"
     android:scrollingCache="true" >
    

    , and since none of your listview edittext is asking for focus,, the focus stays on just the listview not the edittext..so you need to re-surface the focus back to the edittext..also from the android documentation it states By default the user can not move focus to a view;.. and also it states that "if focusableInTouchMode is true for a view, that view can gain focus when clicked on, and can keep focus if another view is clicked on that doesn't have this attribute set to true. " mostly when you first create an edittext it automatically adds <requestFocus /> to it specifying that it needs to take focus, or it can take focus..but in your case and with this documentaion the edittext will never keep focus because listview is taking it away from him.. so in short try this on all views(including your edittext) you want to take focus like you did on the listview

    android:focusable="true" 
    android:focusableInTouchMode="true" 
    

    so try that on the edittext and let me know if it helps..thanks for waiting

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