android基礎(chǔ)教程之a(chǎn)ndroid的listview與edittext沖突解決方法
最近遇到一個(gè)關(guān)于android軟鍵盤的問題。在ListView中每個(gè)Item中都有個(gè)EditText,在最后的幾個(gè)Item中,EditText第一次點(diǎn)擊界面還能向上彈出,正常顯示,
但第二次點(diǎn)擊時(shí),軟件盤就把最后的幾個(gè)Item給正當(dāng)住了。這樣很影響用戶體驗(yàn)的。
其實(shí)解決的辦法只要想一下,我相信有經(jīng)驗(yàn)的開發(fā)人員就能夠想到,讓軟鍵盤在消失的時(shí)候讓相應(yīng)Item中的EditText消失焦點(diǎn)clearFouce();但是有個(gè)關(guān)鍵的問題,
就是在獲得返回事件的時(shí)候,如果獲得的事件不對(duì)那就不會(huì)達(dá)到想要的效果。這個(gè)back時(shí)間一定要是自定Layout中的back事件才可以。
直接上代碼。
<cn.test.systemSetting.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_bg"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_data"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:transcriptMode="normal"
>
</ListView>
</cn.test.systemSetting.MyLayout>
自定義layout中所作的處理:
package cn.test.systemSetting;
import com.********.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
/**
*
* 針對(duì)設(shè)備管理鍵盤事件的處理
* divid小碩
*
* **/
public class MyLayout extends LinearLayout {
private Context context;
public MyLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context=context;
LayoutInflater.from(context).inflate(R.layout.device_manager, this);//此處所加載的layout就是上面的xml,即它的名字就是device_manager.xml
}
public MyLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
// TODO Auto-generated method stub
if(context!=null){
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive() && event.getKeyCode() == KeyEvent.KEYCODE_BACK){
View view = DeviceManagerActivity.lv_data.getFocusedChild();
if(view!=null){
view.clearFocus();
}
}
}
return super.dispatchKeyEventPreIme(event);
}
}
主界面所采用的加載方式要是這樣的:
public class DeviceManagerActivity extends Activity implements OnClickListener{
public static ListView lv_data;
static DevMgrAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1.全屏
requestWindowFeature(Window.FEATURE_NO_TITLE); // 無標(biāo)題
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(new MyLayout(this));
init();
}
}
- Android取消EditText自動(dòng)獲取焦點(diǎn)默認(rèn)行為
- Android控件系列之EditText使用方法
- android同時(shí)控制EditText輸入字符個(gè)數(shù)和禁止特殊字符輸入的方法
- Android中EditText實(shí)現(xiàn)不可編輯解決辦法
- Android定制自己的EditText輕松改變底線顏色
- Android編程設(shè)置TextView顏色setTextColor用法實(shí)例
- Android更改EditText下劃線顏色樣式的方法
- Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤
- 全面解析Android中對(duì)EditText輸入實(shí)現(xiàn)監(jiān)聽的方法
- Android中EditText setText方法的踩坑實(shí)戰(zhàn)
相關(guān)文章
android 仿微信demo——登錄功能實(shí)現(xiàn)(服務(wù)端)
這系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗?,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06Android 大文件切割與合并的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 大文件切割與合并,實(shí)現(xiàn)了很多發(fā)文件和視頻的切割,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11Android-自定義控件之ListView下拉刷新的實(shí)現(xiàn)
本篇文章主要介紹了Android-自定義控件之ListView下拉刷新示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Android第三方HTTP網(wǎng)絡(luò)支持包OkHttp的基礎(chǔ)使用教程
在GitHub上開源的安卓HTTP編程包OkHttp正在積累著越來越高的人氣,這里我們就來看一下這款A(yù)ndroid第三方HTTP網(wǎng)絡(luò)支持包OkHttp的基礎(chǔ)使用教程:2016-07-07Android實(shí)現(xiàn)朋友圈評(píng)論回復(fù)列表
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)朋友圈評(píng)論回復(fù)列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11