我正在使用一个可扩展的列表视图,其子布局包含 1 个 EditText、2 个 ToggleButtons 和 1 个搜索栏。我能够使 EditText 成为焦点并弹出键盘,我可以在 EditText 中输入任何内容,但是当我向下滚动,远离焦点 EditText 或从可扩展列表视图打开一个新组时,文本我写回预设文本。
切换按钮和搜索栏都不会恢复到之前的状态。
我尝试在 xml 中添加 android:descendantFocusability="beforeDescendants" 和 android:descendantFocusability="afterDescendants" 但没有成功。在 AndroidManifest 中添加 android:windowSoftInputMode="adjustPan"> 帮助我解决了我之前的问题,即没有让键盘弹出但只在一瞬间闪烁。
我的 MainActivity 看起来像这样:
public class MainActivity extends ExpandableListActivity {
private List<EditText> editTextList = new ArrayList<EditText>();
public FileOutputStream fOut;
public String str;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
EditText editText;
String temp;
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
editText = (EditText) findViewById(R.id.grp_child);
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int count = 200;
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(), // Creating group List.
R.layout.group_row, // Group item layout XML.
new String[] { "Turn" }, // the key of group item.
new int[] { R.id.row_name }, // ID of each group item.-Data under the key goes into this TextView.
createChildList(), // childData describes second-level entries.
R.layout.child_row, // Layout for sub-level entries(second level).
new String[] {"Sub Item"}, // Keys in childData maps to display.
new int[] { R.id.grp_child} // Data under the keys above go into these TextViews.
);
setListAdapter( expListAdapter ); // setting the adapter in the list.
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
}
/* Creating the Hashmap for the row */
@SuppressWarnings("unchecked")
private List createGroupList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < 200 ; ++i ) { // 15 groups........
HashMap m = new HashMap();
m.put( "Turn","Turn" + i ); // the key and it's value.
result.add( m );
}
return (List)result;
}
/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < 200 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
/* each group need each HashMap-Here for each group we have 3 subgroups */
ArrayList secList = new ArrayList();
for( int n = 0 ; n < 1 ; n++ ) {
HashMap child = new HashMap();
child.put( "Sub Item", "Sub Item " + n );
secList.add( child );
}
result.add( secList );
}
return result;
}
public void onContentChanged () {
System.out.println("onContentChanged");
super.onContentChanged();
}
private View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View view) {
StringBuilder stringBuilder = new StringBuilder();
for (EditText editText : editTextList) {
stringBuilder.append(editText.getText().toString());
}
}
};
/* This function is called on each child click */
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
return true;
}
/* This function is called on expansion of the group */
public void onGroupExpand (int groupPosition) {
try{
System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
}catch(Exception e){
System.out.println(" groupPosition Errrr +++ " + e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit")
.setMessage(
"(Do not forget to save before exiting)"
+ "\nAre you sure you want to exit?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
finish();
System.exit(0);
}
}).setNegativeButton("No", null).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
StringBuilder stringBuilder = new StringBuilder();
for (EditText editText : editTextList) {
stringBuilder.append(editText.getText().toString());
}
String[] items = new String[editTextList.size()];
for (int i = 0; i < editTextList.size(); i++) {
items[i] = editTextList.get(i).getText().toString();
}
File path = new File(Environment.getExternalStorageDirectory(),
"/text");
if (!path.exists()) {
path.mkdirs();
}
try {
File text = new File(path, dateFormat.format(date) + ".txt");
FileWriter writer = new FileWriter(text);
for (String str : items) {
writer.write(str);
}
writer.flush();
writer.close();
System.out.println("Nu fungerar det");
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "Save is Selected",
Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_about:
Toast.makeText(
MainActivity.this,
"This app is made as a tool to capture data of game sessions of the City Game",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}