标签:
activity布局:布局很简单,只有一个listview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.testlistviewandedittext.MainActivity" > <ListView android:id="@+id/lv_main" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > </EditText> </LinearLayout>
package com.example.testlistviewandedittext; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Adapter; import android.widget.ListView; public class MainActivity extends Activity { private Context context; private ListView lv_main; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { // TODO Auto-generated method stub context = MainActivity.this; lv_main = (ListView) findViewById(R.id.lv_main); adapter = new MyAdapter(context, getStringList()); lv_main.setAdapter(adapter); } private List<String> getStringList() { List<String> list = new ArrayList<String>(); for(int i=0;i<30;i++) { list.add("我是第"+i+"个cell"); } return list; } }然而,重点是在这里....
adapter的代码:
package com.example.testlistviewandedittext; import java.util.HashMap; import java.util.List; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; public class MyAdapter extends BaseAdapter { private Context context; private List<String> strList; private int index = -1;// 当前获取焦点的位置 private HashMap<Integer, String> contentMap = new HashMap<Integer, String>();// 保存edittext的数据,避免软键盘弹出和隐藏对数据的影响 public MyAdapter(Context context, List<String> strList) { this.context = context; this.strList = strList; } @Override public int getCount() { return strList == null ? 0 : strList.size(); } @Override public String getItem(int position) { return strList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // if (convertView == null) { viewHolder = new ViewHolder(); convertView = View.inflate(context, R.layout.listview_cell, null); final EditText editText = (EditText) convertView .findViewById(R.id.et_content); // viewHolder.et_content = (EditText) convertView // .findViewById(R.id.et_content); // convertView.setTag(viewHolder); // } else { // viewHolder = (ViewHolder) convertView.getTag(); // } if (contentMap.get(position) == null) { contentMap.put(position, getItem(position));// 保存一下 } editText.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { index = position; return false; } }); editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { Log.e("myadapter", "文字内容已经改变,当前文字:" + s.toString()); contentMap.put(position, s.toString());// 把改变后的数据存储起来 } }); if (contentMap.get(position) != null) { Log.e("myadapter", position + "content不为空"); editText.setText(contentMap.get(position)); } else { Log.e("myadapter", position + "content为空"); editText.setText(""); } // 设置焦点和edittext设置光标位置,记住啊.一定要先设置text.然后再设置焦点和光标位置 if (index != -1 && index == position) { editText.requestFocus(); editText.setSelection(editText.getText().toString().length()); } return convertView; } // cell 是edittext的话,如果再复用的话,会导致各种坑..慎重.. private ViewHolder viewHolder; public static class ViewHolder { EditText et_content; } }
哦了,记录一下,
版权声明:本文为博主原创文章,未经博主允许不得转载。
android listview 每一项都是edittext 导致的坑爹问题 内容的保存和焦点,光标位置的设置
标签:
原文地址:http://blog.csdn.net/u010399316/article/details/47355009