![](/img/trans.png)
[英]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.