[英]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.