简体   繁体   English

为什么当我单击键盘上的“确定”按钮时清除了我的EditText?

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

I have a weird problem. 我有一个奇怪的问题。 I have a gridView. 我有一个gridView。 On each item on the GridView, I inflate a Relative Layout with an ImageView and an EditText. 在GridView的每个项目上,我都为一个相对布局添加一个ImageView和一个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). 文本将保存在EditText(整个屏幕上为键盘)上。 But in portait mode, Keyboard take a half screen. 但是在纵向模式下,键盘将占据半个屏幕。 When I write some words, it appears on the EditText. 当我写一些单词时,它会出现在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 : 因此,我的侦听器向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 : 我的适配器上的GetView方法:

@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. 将此内容添加到XML的EditText中,然后键盘将不会占据整个屏幕。

android:imeOptions="flagNoExtractUi"

Through java code 通过java代码

your_editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); 

(1) Open your AndroidManifest.xml (1)打开您的AndroidManifest.xml

(2) In your activity where GridView exist, put android:windowSoftInputMode="adjustPan" (2)在存在GridView的活动中,将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. 我这样解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个活动,其中有edittext,当我单击edittext打开键盘时,应用程序崩溃并重新启动,为什么? - I have an activity in which edittext is there and when I click on edittext to open keyboard the apps crashed and restarted why? Android 自定义 ListView - 当软键盘隐藏时 Edittext 被清除 - Android custom ListView - Edittext gets cleared when the soft keyboard hides 为什么在使用休眠模式时会清除我的表? - Why my Tables get cleared when i use hibernate? 当我点击按钮时,为什么我的挥杆挂起? - why my swing just hangs when I click button? 为什么单击按钮后我的应用程序崩溃? - Why does my app crash when i click the button? 单击按钮后,如何将文本从alertdialog的EditText复制并粘贴到活动的EditText中? - How do I copy and paste text from EditText of alertdialog to EditText of my activity, on button click? 当EditText为空并且按下按钮时,我的应用程序崩溃 - My app crashes when an EditText is empty and I press a button 当我单击数字键盘上的“完成”按钮时,应用程序崩溃 - The app crashes when I click on the “Done” button on numeric keyboard EditText下一个按钮键盘 - EditText next Button keyboard 单击按钮时通过代码在android中放大edittext - Enlarge edittext in android by code when click a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM