首页 文章

edittext中的输入文本将恢复为可扩展列表视图中的预设文本

提问于
浏览
1

我正在使用一个可扩展的列表视图,其子布局包含1个EditText,2个ToggleButtons和1个搜索栏 . 我能够使EditText处于焦点并弹出键盘,我可以在EditText中键入任何内容但是当我向下滚动,远离焦点EditText或从可展开列表视图中打开一个新组时,文本我写的是回复到预设文本 .

togglebuttons和seekbar都不会恢复到之前的状态 .

Prestate

During

After

我试过在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);
    }
}

}

1 回答

  • 0

    我知道这个答案太过分了 . 但是它可能对其他用户有帮助 .

    如果 ListView 扩展到屏幕外, Edittext 通常在 ListView 中无法正常工作 . 当焦点改变时,视图被回收,因此 Edittext 失去了它的 Value . 您可以使用 TableLayout 来获得相同的结果 .

相关问题