我在我的应用程序的活动中使用网格视图.每个网格视图行包含三个复选框,可以根据用户想要从数据库查询的内容将其设置为选择/不选择.该活动还包括一个editText,每次活动开始时都会出现onScreenKeyboard,这里我遇到了问题. OnScreenKeyboard,当出现干扰gridview时,它的一些复选框就会消失.我的想法是每次配置更改时刷新网格视图.我尝试通过onRetainNonConfigurationInstance()返回一个对象来处理这个问题.当我使用getLastNonConfigurationInstance()来检索返回的对象时,Object包含一个数组列表,用于填充我的gridview行但是onCreate(),它显示为null.任何人都可以建议我如何处理这个问题或者是否有任何其他方法可以使我的gridView在配置更改时表现正常.以下是代码,我想说清楚我已经在清单文件中添加了keyboardHidden配置更改但是当键盘出现时有时不会触发onConfigurationChanged()
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.failureselection); findLocalWidgets(); //Initializes Layout Objects if(configchanged){ //Re populate grid view customDataGridRows = (ArrayList<CustomGridViewRow>) getLastNonConfigurationInstance(); dgvwFailures.setAdapter(new CustomGridViewAdapter(this,customDataGridRows)); configchanged = false; }else{ fillFailuresList(customDataGridRows); dgvwFailures.setAdapter(new CustomGridViewAdapter(this,customDataGridRows)); } } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.failureselection); configchanged = true; } @Override public Object onRetainNonConfigurationInstance() { return customDataGridRows; } private boolean fillFailuresList( ArrayList<CustomGridViewRow> customDataGridRows) { boolean isFilled = false; try { // Adding Failures data customDataGridRows.add(new CustomGridViewRow(false,"Hood",false,"Assembly Defect","Masking Poor")); customDataGridRows.add(new CustomGridViewRow(false,"Floor","Forget Work","Assembly Defect")); customDataGridRows.add(new CustomGridViewRow(false,"Grill","Incorrect Assembly","Bad Company")); customDataGridRows.add(new CustomGridViewRow(false,"R Right Frame","Interference","Fix Large Effort")); customDataGridRows.add(new CustomGridViewRow(false,"R Left Frame","Leakage","High Incidence")); customDataGridRows.add(new CustomGridViewRow(false,"R Frame","Dirt","Recurrence")); customDataGridRows.add(new CustomGridViewRow(false,"Outside R Frame","Decal","Checking")); customDataGridRows.add(new CustomGridViewRow(false,"F Right Frame","Other","Foreign Body")); customDataGridRows.add(new CustomGridViewRow(false,"F Left Frame","","Not Caulking")); customDataGridRows.add(new CustomGridViewRow(false,"F Frame","Painting Defect")); customDataGridRows.add(new CustomGridViewRow(false,"Outsie F Frame","Other")); customDataGridRows.add(new CustomGridViewRow(false,"")); // Populating Failures grid view // dgvwFailures.setAdapter(new CustomGridViewAdapter(this,// customDataGridRows)); isFilled = true; } catch (Exception e) { e.getMessage(); } return isFilled; }
解决方法
有两种选择:
> How to prevent Custom Views from losing state across screen orientation changes为您的CustomGridViewRow.
>在适配器中创建一个方法,并将值放在一个包中,并使用savedInstanceState在create上恢复它
小费:
删除任何< requestFocus />从您的EditText布局.虽然Android会将焦点放在第一个可聚焦的视图上.
另一个黑客:How to remove focus without setting focus to another control?