[英]I have an activity in which edittext is there and when I click on edittext to open keyboard the apps crashed and restarted why?
[英]Why my EditText is cleared when I click on “OK” button on the keyboard?
我有一个奇怪的问题。 我有一个gridView。 在GridView的每个项目上,我都为一个相对布局添加一个ImageView和一个EditText。
在横向模式下,它可以正常工作。 我写了一些字,然后单击“确定”。 文本将保存在EditText(整个屏幕上为键盘)上。 但是在纵向模式下,键盘将占据半个屏幕。 当我写一些单词时,它会出现在EditText上。 当我单击键盘上的“确定”按钮时,文本消失。
我不明白为什么。
因此,我的侦听器向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方法:
@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;
}
将此内容添加到XML的EditText
中,然后键盘将不会占据整个屏幕。
android:imeOptions="flagNoExtractUi"
通过java代码
your_editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
(1)打开您的AndroidManifest.xml
(2)在存在GridView的活动中,将android:windowSoftInputMode="adjustPan"
例如
<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>
我这样解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.