android basic tutorial on android's listview and edittext conflict resolution

  • 2020-05-27 07:04:36
  • OfStack

I recently encountered a problem with the android soft keyboard. In ListView, there is an EditText in each Item. In the last few Item, the first click interface of EditText will pop up and display normally.

But on the second click, the software disk holds the last Item. It really affects the user experience.

In fact, the solution is as simple as thinking 1, I believe experienced developers can think of, let the soft keyboard in the disappearance of the corresponding Item EditText focus clearFouce (); But there's a crucial question,

That is, when you get the return event, if you get the wrong event you're not going to get what you want. This back time 1 must be a custom back event in Layout.

Go directly to the code.


<cn.test.systemSetting.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboardlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_bg"
    android:orientation="vertical" >
    <ListView
                android:id="@+id/lv_data"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:cacheColorHint="#00000000"
                android:transcriptMode="normal" 
                >
     </ListView>
</cn.test.systemSetting.MyLayout>

Customize the processing done in layout:


package cn.test.systemSetting;
import com.********.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
/**
 * 
 *  Handling of device management keyboard events 
 * divid Small to recognize 
 * 
 * **/
public class MyLayout extends LinearLayout {
    private Context context;
    public MyLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context=context;
        LayoutInflater.from(context).inflate(R.layout.device_manager, this);// Loaded here layout That's the top xml Its name is device_manager.xml
    }
    public MyLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    
    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        // TODO Auto-generated method stub
        if(context!=null){
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if(imm.isActive() && event.getKeyCode() == KeyEvent.KEYCODE_BACK){
                View view = DeviceManagerActivity.lv_data.getFocusedChild();
                if(view!=null){
                    view.clearFocus();
                }

            }
        }

        return super.dispatchKeyEventPreIme(event);
    }
}

The loading method of the main interface is as follows:


public class DeviceManagerActivity extends Activity implements OnClickListener{
    public static ListView lv_data;
    static DevMgrAdapter adapter;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 1. Full screen 
        requestWindowFeature(Window.FEATURE_NO_TITLE); //  No title 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setContentView(new MyLayout(this));
        init();
    }
}


Related articles: