Issues focusing EditTexts in a ListView (Android)

前端 未结 2 1478
独厮守ぢ
独厮守ぢ 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();
        }
    

提交回复
热议问题