How to prevent Edittext out of focus when it's focused?

568 views Asked by At

I have a Gridview, and each row of it has 3 Edittext. I expect that user can only type the number in those Edittext, so I set input type "numberDecimal." My problem is when I focus on any Edittext, it out of focus immediately, and the soft input keyboard of Android always cover the Numeric keyboard. I tried to add android:windowSoftInputMode="adjustNothing" in manifest in the Activity that contains the Gridview, so my problem's solved but the gridView is not scrollable when I focus on any Edittext in each row of Gridview. So can you suggest me another solution for my problem? please see the GIF I posted to know what I'm talking about, thank you su much ! http://i.imgur.com/NL5knAR.gifv

My custom Gridview row code :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lnRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border_bang_diem"
    android:orientation="horizontal"
    android:weightSum="120.5"

    >

<TextView
    android:id="@+id/txtTM"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="15"
    android:background="@drawable/border_bang_diem"
    android:gravity="center_vertical"
    android:maxLines="2"
    android:paddingBottom="5dp"
    android:paddingLeft="4dp"
    android:paddingTop="5dp" />

<TextView
    android:id="@+id/txtST"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="22"
    android:background="@drawable/border_bang_diem"
    android:gravity="center"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" />

<EditText
    android:id="@+id/edtCC"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="21"
    android:background="@drawable/border_bang_diem"
    android:gravity="center"
    android:hint="@string/not"
    android:inputType="numberDecimal"
    android:maxLength="4"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:selectAllOnFocus="true" />

<EditText
    android:id="@+id/edtGK"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="21"
    android:background="@drawable/border_bang_diem"
    android:gravity="center"
    android:hint="@string/not"
    android:inputType="numberDecimal"
    android:maxLength="4"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:selectAllOnFocus="true" />

<EditText
    android:id="@+id/edtKTHP"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="21"
    android:background="@drawable/border_bang_diem"
    android:gravity="center"
    android:hint="@string/not"
    android:inputType="numberDecimal"
    android:maxLength="4"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:selectAllOnFocus="true" />

<TextView
    android:id="@+id/txtDiemTB"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="20.5"
    android:background="@drawable/border_bang_diem"
    android:gravity="center"
    android:hint="@string/not"
    android:maxLength="4"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" />
</LinearLayout>

My Activity that contain the Gridview code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">

    <ScrollView
        android:fillViewport="true"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >

        <LinearLayout
            android:orientation="vertical"
            android:weightSum="109.5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="31"
                android:background="@drawable/quan_ly_diem_background"
                android:orientation="horizontal"
                android:weightSum="10">

                <ImageView
                    android:id="@+id/imganh"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="3.8"
                    android:padding="6dp"
                    android:src="@drawable/logokma_final" />

                <LinearLayout

                    android:id="@+id/LnTen"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="3"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/txtchumasv"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Mã sinh viên :"
                        android:textColor="#111"
                        android:textSize="@dimen/sp13" />

                    <TextView
                        android:id="@+id/txtmasv"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/border_bang_diem"
                        android:maxLines="1"
                        android:padding="@dimen/dp10"
                        android:text="@string/ma"
                        android:textColor="#111"
                        android:textSize="@dimen/dp15"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="3.2"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5"
                        android:gravity="bottom|center"
                        android:maxLines="1"
                        android:text="@string/quan_ly_diem"
                        android:textColor="#072bcc"
                        android:textSize="@dimen/dp20"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5"
                        android:gravity="top|center"
                        android:maxLines="1"
                        android:text="@string/Bangdiem"
                        android:textColor="#072bcc"
                        android:textSize="@dimen/dp20"
                        android:textStyle="bold" />

                </LinearLayout>


            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="32.5"
                android:background="@drawable/qldiem_2_background"
                android:orientation="horizontal"
                android:weightSum="120.5">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="15"
                    android:background="@drawable/border_bang_diem"
                    android:gravity="center"
                    android:padding="@dimen/dp5"
                    android:text="@string/tenmon"
                    android:textColor="#111"
                    android:textStyle="bold" />

                <TextView
                    android:textSize="13sp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="22"
                    android:background="@drawable/border_bang_diem"
                    android:gravity="center"
                    android:padding="@dimen/dp5"
                    android:text="@string/st"
                    android:textColor="#111"
                    android:textStyle="bold" />


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="21"
                    android:background="@drawable/border_bang_diem"
                    android:gravity="center"
                    android:maxLines="2"
                    android:text="@string/cc1"
                    android:textColor="#111"
                    android:textSize="13sp"
                    android:textStyle="bold" />


                <TextView
                    android:textSize="13sp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="21"
                    android:background="@drawable/border_bang_diem"
                    android:gravity="center"
                    android:padding="@dimen/dp5"
                    android:text="@string/gk"
                    android:textColor="#111"
                    android:textStyle="bold" />

                <TextView
                    android:textSize="13sp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="21"
                    android:background="@drawable/border_bang_diem"
                    android:gravity="center"
                    android:padding="@dimen/dp5"
                    android:text="@string/KTHP"
                    android:textColor="#111"
                    android:textStyle="bold" />

                <TextView
                    android:textSize="13sp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="20.5"
                    android:background="@drawable/border_bang_diem"
                    android:gravity="center"
                    android:padding="@dimen/dp5"
                    android:text="@string/TB"
                    android:textColor="#111"
                    android:textStyle="bold" />
            </LinearLayout>


            <GridView
                android:id="@+id/gvDiem"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="12.5"
                android:numColumns="1"></GridView>


            <LinearLayout
                android:id="@+id/LnDiemTK"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="33.5"
                android:background="@color/colorLow"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:gravity="center"
                    android:text="@string/DiemTK"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txtDiemTK"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="6"
                    android:gravity="center"
                    android:maxLength="4"
                    android:text="@string/not"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>



</LinearLayout>

My custom adapter class for Gridview :

public class CustomDiemAdapter extends ArrayAdapter<DiemDTO> {

    Context context;
    ArrayList<DiemDTO> listDiemDTO;
    int layout;

    TextView txtTenMon, txtSoTin, txtDiemTB;
    EditText edtDiemCC, edtDiemGK, edtDiemKTHP;



    public CustomDiemAdapter(Context context, int layout, ArrayList<DiemDTO> listDiemDTO) {
        super(context, layout, listDiemDTO);
        this.context = context;
        this.layout = layout;
        this.listDiemDTO = listDiemDTO;
        //diemDAO = new DiemDAO(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DiemDTO diemDTO = this.listDiemDTO.get(position);

            convertView = inflater.inflate(this.layout, parent, false);
           // viewHolderDiem = new ViewHolderDiem();



            txtTenMon = (TextView) convertView.findViewById(R.id.txtTM);
            txtSoTin = (TextView) convertView.findViewById(R.id.txtST);

            edtDiemCC = (EditText) convertView.findViewById(R.id.edtCC);
            edtDiemCC.addTextChangedListener(new MyTextWatcher(edtDiemCC, diemDTO, convertView));

            edtDiemGK = (EditText) convertView.findViewById(R.id.edtGK);
            edtDiemGK.addTextChangedListener(new MyTextWatcher(edtDiemGK, diemDTO, convertView));

            edtDiemKTHP = (EditText) convertView.findViewById(R.id.edtKTHP);
            edtDiemKTHP.addTextChangedListener(new MyTextWatcher(edtDiemKTHP, diemDTO, convertView));

            txtDiemTB = (TextView) convertView.findViewById(R.id.txtDiemTB);


        if (position % 2 == 0) {
            convertView.setBackgroundColor(Color.rgb(238, 233, 233));
        }

        txtTenMon.setText(diemDTO.getTENMH());
        txtSoTin.setText(String.valueOf(diemDTO.getSoTin()));


        if (String.valueOf(diemDTO.getDIEMCC()).equalsIgnoreCase("0.0")){
            edtDiemCC.setText("");
            edtDiemCC.addTextChangedListener(new MyTextWatcher(edtDiemCC, diemDTO, convertView));
            edtDiemCC.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
        }else{

            edtDiemCC.setText(String.valueOf(diemDTO.getDIEMCC()));
            edtDiemCC.addTextChangedListener(new MyTextWatcher(edtDiemCC, diemDTO, convertView));
            edtDiemCC.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
        }


        if (String.valueOf(diemDTO.getDIEMGK()).equalsIgnoreCase("0.0")){
            edtDiemGK.setText("");
            edtDiemGK.addTextChangedListener(new MyTextWatcher(edtDiemGK,diemDTO,convertView));
            edtDiemGK.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
        }
        else {
            edtDiemGK.setText(String.valueOf(diemDTO.getDIEMGK()));
            edtDiemGK.addTextChangedListener(new MyTextWatcher(edtDiemGK,diemDTO,convertView));
            edtDiemGK.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
        }



        if (String.valueOf(diemDTO.getDIEMKTHP()).equalsIgnoreCase("0.0")){
            edtDiemKTHP.setText("");
            edtDiemKTHP.addTextChangedListener(new MyTextWatcher(edtDiemKTHP,diemDTO,convertView));
            edtDiemKTHP.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
        }
        else {
            edtDiemKTHP.setText(String.valueOf(diemDTO.getDIEMKTHP()));
            edtDiemKTHP.addTextChangedListener(new MyTextWatcher(edtDiemKTHP,diemDTO,convertView));
            edtDiemKTHP.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
        }


        txtDiemTB.setText(String.valueOf(diemDTO.getDIEMTB()));

        return convertView;
    }

    class MyTextWatcher implements TextWatcher {
        EditText view;
        DiemDTO diemDTO;
        View convertView;
        DiemDAO diemDAO = new DiemDAO(context);
        public MyTextWatcher(EditText view, DiemDTO diemDTO, View convertView) {
            this.view = view;
            this.diemDTO = diemDTO;
            this.convertView = convertView;
        }

        @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) {
            String text = s.toString();

            // phai Focus vao truoc sau do moi so sanh length
            //diemDAO = new DiemDAO(context);
            TextView txtTB = (TextView) this.convertView.findViewById(R.id.txtDiemTB);
            if (view.getText().toString().length() > 0) {
                switch (view.getId()) {
                    case R.id.edtCC:
                        if (view.isFocused()) {
                            view.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
                            this.diemDTO.setDIEMCC(Float.parseFloat(text));
                            diemDAO.updateDiem(this.diemDTO);
                            if(diemDAO.sosanh1() == diemDAO.sosanh2()){
                                diemDTO.setDIEMTK(diemDAO.TinhDiemTK(diemDTO.getMASV()));
                            }
                            else{
                                diemDTO.setDIEMTK(0.0f);
                            }
//                            diemDTO.setDIEMTK(diemDAO.TinhDiemTK(diemDTO.getMASV()));
                        }
                        break;
                    case R.id.edtGK:
                        if (view.isFocused()) {
                            view.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
                            this.diemDTO.setDIEMGK(Float.parseFloat(text));
                            diemDAO.updateDiem(this.diemDTO);
                            if(diemDAO.sosanh1() == diemDAO.sosanh2()){
                                diemDTO.setDIEMTK(diemDAO.TinhDiemTK(diemDTO.getMASV()));
                            }
                            else{
                                diemDTO.setDIEMTK(0.0f);
                            }
//                            diemDTO.setDIEMTK(diemDAO.TinhDiemTK(diemDTO.getMASV()));
                        }
                        break;
                    case R.id.edtKTHP:
                        if (view.isFocused()) {
                            view.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 10.0f)});
                            this.diemDTO.setDIEMKTHP(Float.parseFloat(text));
                            diemDAO.updateDiem(this.diemDTO);
                            if(diemDAO.sosanh1() == diemDAO.sosanh2()){
                                diemDTO.setDIEMTK(diemDAO.TinhDiemTK(diemDTO.getMASV()));
                            }
                            else{
                                diemDTO.setDIEMTK(0.0f);
                            }
//                            diemDTO.setDIEMTK(diemDAO.TinhDiemTK(diemDTO.getMASV()));
                        }
                        break;
                }
            }
            txtTB.setText(String.valueOf(this.diemDTO.getDIEMCC() * 0.1f + this.diemDTO.getDIEMGK() * 0.2f + 0.7f * this.diemDTO.getDIEMKTHP()));
            this.diemDTO.setDIEMTB(this.diemDTO.getDIEMCC() * 0.1f + this.diemDTO.getDIEMGK() * 0.2f + 0.7f * this.diemDTO.getDIEMKTHP());

            View rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
            TextView txtDiemTK = (TextView) rootView.findViewById(R.id.txtDiemTK);
            diemDAO.updateDiem(this.diemDTO);
            txtDiemTK.setText(String.valueOf(DiemDTO.getDIEMTK()));



        }
    }
}
0

There are 0 answers