Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android EditText 用法及焦點處理總結

Android EditText 用法及焦點處理總結

編輯:關於Android編程

筆者最經最近在做一個項目,主要是在Android平板上處理掃描槍數據,

用EditText做字符堅挺,但在EditText焦點用法碰到了一些問題,故而把這些問題及處理方法總結一下

1.如何監聽EditText字符變化

首先想到的是addTextChangedListener事件,但筆者要做的是處理掃描槍以\n結束的數據,並在下一次掃描時清除掉上一次的數據

具體代碼如下:

private boolean bEnter = false;

EditSop.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence arg0, int start, int before, int count) {
String str = arg0.toString();
int Len = str.length();
if(bEnter){
bEnter = false;
if( ! str.endsWith("\n") ) {
String strBuff = str.substring(Len-1, Len);
EditSop.setText(strBuff);
EditSop.setSelection(1);
}

}
if(str.endsWith("\n")){
String str1 = str.substring(0, Len-1);
int iPos = str1.lastIndexOf("\n");
String strBuff = "";
if(-1 == iPos) {
strBuff = str1.substring(0, str1.length());
}else{
strBuff = str1.substring(iPos+1, str1.length());
}
//Toast.makeText(MainActivity.this, strBuff, 2000).show();
//mbarcode = strBuff;
mDialog.setMessage("正在掃描條碼...");
mDialog.show();
RScan rScan = new RScan(strBuff);
Thread thread = new Thread(rScan);
thread.start();
EditSop.setText(str.substring(0, Len-1));
EditSop.setSelection(Len-1);
bEnter = true;
}

//Toast.makeText(MainActivity.this, "start: "+start+"before: "+before+"count: "+count, 2000).show();

}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {Toast.makeText(MainActivity.this, "start: "+arg1+"before: "+arg2+"count: "+arg3, 2000).show();}

@Override
public void afterTextChanged(Editable arg0) {
//Toast.makeText(MainActivity.this, arg0.toString(), 2000).show();
}
});


2.如何鎖定EditText光標一直處於最後

聯想到setOnClickListener就很容易處理了,代碼如下:

EditSop.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
int Len = EditSop.getEditableText().toString().length();
EditSop.setSelection(Len);
}
});


最後筆者在edittext與listview焦點處理上一直有問題,還需要繼續研究...

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved