简体   繁体   中英

Why my EditText is cleared when I click on “OK” button on the keyboard?

I have a weird problem. I have a gridView. On each item on the GridView, I inflate a Relative Layout with an ImageView and an EditText.

On landscape Mode, it works fine. I write some words and click on "OK". The text is kept on the EditText (Keyboard take the whole screen). But in portait mode, Keyboard take a half screen. When I write some words, it appears on the EditText. When I click on "OK" button on the KeyBoard, the Text disappear.

I don't understand why.

So my listener to add an item to the GridView :

catAddButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Add a new empty SubCategory
        subcatList.add(new SubCategory());
        //Set toggle true to say that add Button was clicked.
        adapter.setToggle(true);
        //Notify adapter that Data changed
        adapter.notifyDataSetChanged();
        //Scroll to the last added SubCategory
        gridCat.smoothScrollToPosition(gridCat.getCount() - 1);

        //Show buttons.
        validateButton.setVisibility(View.VISIBLE);
        cancelButton.setVisibility(View.VISIBLE);
        catAddButton.setClickable(false);
    }
});

GetView method on my adapter :

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //If toggle is true so regenerate after clicked on Add Button and this view is the last one
        if (isToggle() && position == listSubCat.size() - 1) {
                //Inflate new view
                convertView = mInflater.inflate(R.layout.subcat_view, null);

                holder = new ViewHolder();
                //Get TextView etc... into Holder
                holder.SubCatName = (EditText) convertView.findViewById(R.id.subcatName);
                holder.imageSubCat = (ImageView) convertView.findViewById(R.id.imageSubCatView);

                //Set a tag
                convertView.setTag(holder);

                //Add textWatcher to save value of EditText
                holder.SubCatName.addTextChangedListener(saveEditText);

                //Get SubCat
                SubCategory subCat = (SubCategory) getItem(position);
                //If SubCat exists
                if (subCat != null) {
                    holder.SubCatName.setFocusableInTouchMode(true);
                    holder.SubCatName.setFocusable(true);
                    holder.SubCatName.requestFocus();

                    /*holder.SubCatName.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() != KeyEvent.ACTION_DOWN)
                                return false;
                            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                                holder.SubCatName.setText(holder.SubCatName.getText());
                                return true;
                            }
                            return false;
                        }
                    });*/
                    //TODO test if keyboard appear
                    //KeyBoardTools.showKeyBoard((Activity)context);
                    //Set data into holder
                    holder.imageSubCat.setImageDrawable(context.getResources().getDrawable(R.drawable.subcat_default));
                }
        }
        //If toggle is false
        else {
            //If we can't recycle a view
            if (convertView == null) {
                //Inflate new view
                convertView = mInflater.inflate(R.layout.subcat_view, null);

                holder = new ViewHolder();
                //Get TextView etc... into Holder
                holder.SubCatName = (EditText) convertView.findViewById(R.id.subcatName);
                holder.imageSubCat = (ImageView) convertView.findViewById(R.id.imageSubCatView);

                //Set a tag
                convertView.setTag(holder);
            }
            else {
                //Get the older by TAG
                holder = (ViewHolder) convertView.getTag();
            }

            holder.SubCatName.removeTextChangedListener(saveEditText);

            //Get SubCat
            SubCategory subCat = (SubCategory) getItem(position);
            //If SubCat exists
            if (subCat != null) {
                //Set data into holder
                holder.SubCatName.setFocusableInTouchMode(false);
                holder.SubCatName.setFocusable(false);
                holder.SubCatName.setText(subCat.getName());
                holder.imageSubCat.setImageDrawable(context.getResources().getDrawable(R.drawable.subcat_default));
            }
        }
        return convertView;
    }

Add this to your EditText in XML, then keyboard will not take whole screen.

android:imeOptions="flagNoExtractUi"

Through java code

your_editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); 

(1) Open your AndroidManifest.xml

(2) In your activity where GridView exist, put android:windowSoftInputMode="adjustPan"

example ,

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:windowSoftInputMode="adjustPan"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

I solved my problem that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM