Android开发之ListView+EditText-要命的焦点问题终极解决方案
2015-07-22 11:41
826 查看
很多人在处理listview与edittext的焦点问题时候,会发现软键盘重新加载的问题,在网上看了一些解决方案,觉得可行,但是在我把edittext设置成数字输入的时候问题又出现了,因为软键盘的出现。listview会被加载两次,但是第二次其实中间有个间隙,在这个间隙中,会有一次默认软键盘弹出(也就是会弹出输入拼音的那个界面,这样的话就会看到软键盘弹出两次,用户体验不是很好)综上,想了一会,一不小心想到个办法试了一下,结果可行
原始理论借鉴于这里,说的很详细,我就不细说了 只说重点处http://blog.csdn.net/l_serein/article/details/7524702 但是这里有个错误 一定要注意 下面会说明这个错误点
以下代码志勇注意加红打字即可,简单好用(主要是因为对于一些指定了软键盘输入类型的输入框所需要解决的问题)
public View getView(final int position, View convertView, ViewGroup parent) {
CarListViewCache viewCache;
if (convertView == null) {
convertView = new LinearLayout(activity);
} else {
//newnumber这里为一个全局变量
if (newnumber!=null) {
newnumber.requestFocus();
}
((LinearLayout) convertView).removeAllViews();
//
viewCache = (CarListViewCache) convertView.getTag();
}
final GoodsList imageAndText = getItem(position);
LayoutInflater inflater = activity.getLayoutInflater();
childView = inflater.inflate(R.layout.car_activity_item, null);
viewCache = new CarListViewCache(childView);
viewCache.describe = (TextView) convertView
.findViewById(R.id.car_activity_unitprice);
viewCache.number = (EditText) convertView
.findViewById(R.id.car_activity_number);
viewCache.name = (TextView) convertView
.findViewById(R.id.car_activity_name);
convertView.setTag(viewCache);
// Load the image and set it on the ImageView
// Set the text on the TextView
TextView name = viewCache.getNameView();
name.setText(imageAndText.ge
4000
tName());
number = viewCache.getNumberView();
number.setText("" + imageAndText.getNumber());
number.setTag(position+"");//这里不多解释了,自己看吧
TextView price = viewCache.getDescribeView();
price.setText("¥" + imageAndText.getPrice());
((LinearLayout) convertView).addView(childView, fillParentLayoutParams);
number.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View
view, MotionEvent event) {
// 在TOUCH的UP事件中,要保存当前的行下标,因为弹出软键盘后,整个画面会被重画
// 在getView方法的最后,要根据index和当前的行下标手动为EditText设置焦点
if (event.getAction() == MotionEvent.ACTION_UP) {
//index = Integer.parseInt(number.getTag().toString());//这个是错误的,更不要写成index
= postion;那你就是2,我承认我2过了
index = Integer.parseInt(view.getTag().toString());//这里一定要注意view.getTag().toString()是上面onTouch(View view, MotionEvent event)传进来的,不然是会有错乱的 这也就是我说的上面地址的错误所在,为了这个我白花了三个小时
newnumber = (EditText) view;这里也最好这样去获取,如果直接number =newnumber ;应该也是可以的,但是个人觉得这样更合理,原因就是上面的一句
}
return false;
}
});
下面两端函数 一个都不能少 一个是为了解决定位问题,另一个是为了解决锁屏后重新解锁后悔弹出一个默认软键盘
之前也说过是因为制定了输入框的输入类型才引发的这些问题,所以下面两行必须有
//锁屏问题的解决方案
if (index!=-1&&(index==position)) {
number.requestFocus();
}
// //重新定位的解决方案
if (newnumber!=null) {
newnumber.requestFocus();
}
return convertView;
}
}
原始理论借鉴于这里,说的很详细,我就不细说了 只说重点处http://blog.csdn.net/l_serein/article/details/7524702 但是这里有个错误 一定要注意 下面会说明这个错误点
以下代码志勇注意加红打字即可,简单好用(主要是因为对于一些指定了软键盘输入类型的输入框所需要解决的问题)
public View getView(final int position, View convertView, ViewGroup parent) {
CarListViewCache viewCache;
if (convertView == null) {
convertView = new LinearLayout(activity);
} else {
//newnumber这里为一个全局变量
if (newnumber!=null) {
newnumber.requestFocus();
}
((LinearLayout) convertView).removeAllViews();
//
viewCache = (CarListViewCache) convertView.getTag();
}
final GoodsList imageAndText = getItem(position);
LayoutInflater inflater = activity.getLayoutInflater();
childView = inflater.inflate(R.layout.car_activity_item, null);
viewCache = new CarListViewCache(childView);
viewCache.describe = (TextView) convertView
.findViewById(R.id.car_activity_unitprice);
viewCache.number = (EditText) convertView
.findViewById(R.id.car_activity_number);
viewCache.name = (TextView) convertView
.findViewById(R.id.car_activity_name);
convertView.setTag(viewCache);
// Load the image and set it on the ImageView
// Set the text on the TextView
TextView name = viewCache.getNameView();
name.setText(imageAndText.ge
4000
tName());
number = viewCache.getNumberView();
number.setText("" + imageAndText.getNumber());
number.setTag(position+"");//这里不多解释了,自己看吧
TextView price = viewCache.getDescribeView();
price.setText("¥" + imageAndText.getPrice());
((LinearLayout) convertView).addView(childView, fillParentLayoutParams);
number.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View
view, MotionEvent event) {
// 在TOUCH的UP事件中,要保存当前的行下标,因为弹出软键盘后,整个画面会被重画
// 在getView方法的最后,要根据index和当前的行下标手动为EditText设置焦点
if (event.getAction() == MotionEvent.ACTION_UP) {
//index = Integer.parseInt(number.getTag().toString());//这个是错误的,更不要写成index
= postion;那你就是2,我承认我2过了
index = Integer.parseInt(view.getTag().toString());//这里一定要注意view.getTag().toString()是上面onTouch(View view, MotionEvent event)传进来的,不然是会有错乱的 这也就是我说的上面地址的错误所在,为了这个我白花了三个小时
newnumber = (EditText) view;这里也最好这样去获取,如果直接number =newnumber ;应该也是可以的,但是个人觉得这样更合理,原因就是上面的一句
}
return false;
}
});
下面两端函数 一个都不能少 一个是为了解决定位问题,另一个是为了解决锁屏后重新解锁后悔弹出一个默认软键盘
之前也说过是因为制定了输入框的输入类型才引发的这些问题,所以下面两行必须有
//锁屏问题的解决方案
if (index!=-1&&(index==position)) {
number.requestFocus();
}
// //重新定位的解决方案
if (newnumber!=null) {
newnumber.requestFocus();
}
return convertView;
}
}
相关文章推荐
- Android Studio最新配置教程2016
- 使用surfaceview做一個簡單要骰子功能
- android activity 的四种启动模式
- Android 手机震动功能实现
- Gradle DSL method not found: 'android()
- Android 之短信验证码自动填写
- Android应用中返回键的监听及处理
- Android-PullToRefresh(二)Android-PullToRefresh详解
- Android 主界面长按创建快捷方式
- Android Studio创建项目
- Android中处理大图片时图片压缩
- Android中处理大图片时图片压缩
- android自定义控件
- Android监听事件添加动作的四种方式
- Android 离线播放 (HLS)m3u8文件
- Android Studio 快捷方式
- android内存泄露优化总结
- Android 代码混淆
- 小超的实习生涯之ImageLoader
- Android 软键盘盖住输入框的问题