我有一个奇怪的问题。我有一个gridView。在GridView上的每个项目上,我使用ImageView和EditText来扩展相对布局。
在横向模式下,它可以正常工作。我写了一些单词并点击" OK"。文本保存在EditText上(键盘占据整个屏幕)。但在肖像模式下,键盘需要半屏。当我写一些单词时,它会出现在EditText上。当我点击" OK"按键在KeyBoard上,文字消失。
我不明白为什么。
所以我的监听器将一个项添加到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;
}
答案 0 :(得分:0)
以XML格式将此EditText
添加到您的android:imeOptions="flagNoExtractUi"
,然后键盘将无法全屏显示。
your_editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
通过java代码
{{1}}
答案 1 :(得分:0)
(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>
我这样解决了我的问题。