androidedittext焦點
❶ android中edittex焦點設置和彈不彈出輸入法的問題
一:EditText為什麼會默認彈出輸入法?
同樣的代碼,碰到有EditText控制項的界面時有的機子會彈出輸入法,有的機子不會彈出。不好意思,這問題我也一頭霧水,誰知道可以告訴我,否則我就把這個問題留下來,以後研究android源碼時再搞個清楚。但是...我有解決方案。
二:默認彈出和默認關閉輸入法的解決方案。
1.默認關閉,不至於進入Activity就打開輸入法,影響界面美觀。
①在布局文件中,在EditText前面放置一個看不到的LinearLayout,讓他率先獲取焦點:
[html] view plainprint?
01.<LinearLayout
02.
03.android:focusable="true"
04.
05.android:focusableInTouchMode="true"
06.
07.android:layout_width="0px"
08.
09.android:layout_height="0px"/>
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
②方法二:先看一個屬性android:inputType:指定輸入法的類型,int類型,可以用|選擇多個。取值可以參考:android.text.InputType類。取值包括:text,textUri,
phone,number,等.
Android SDK中有這么一句話「If
the given content type is TYPE_NULL then a soft keyboard will not be displayed for this text view」,
先將EditText的InputType改變為TYPE_NULL,輸入法就不會彈出.然後再設置監聽,再重新設置它的InputType.
[java] view plainprint?
01.editText.setOnTouchListener(new OnTouchListener() {
02. public boolean onTouch(View v, MotionEvent event) {
03. // TODO Auto-generated method stub
04. int inType = editText.getInputType(); // backup the input type
05. editText.setInputType(InputType.TYPE_NULL); // disable soft input
06. editText.onTouchEvent(event); // call native handler
07. editText.setInputType(inType); // restore input type
08. return true;
09. }
10. });
editText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int inType = editText.getInputType(); // backup the input type
editText.setInputType(InputType.TYPE_NULL); // disable soft input
editText.onTouchEvent(event); // call native handler
editText.setInputType(inType); // restore input type
return true;
}
});
2.默認彈出。有時候按照需求可能要求默認彈出輸入法。方案如下:
[java] view plainprint?
01.EditText titleInput = (EditText) findViewById(R.id.create_edit_title);titleInput.setFocusable(true);
02.
03.titleInput.requestFocus();
04. onFocusChange(titleInput.isFocused());
05.
06.private void onFocusChange(boolean hasFocus)
07.{
08.final boolean isFocus = hasFocus;
09.(new Handler()).postDelayed(new Runnable() {
10.public void run() {
11.InputMethodManager imm = (InputMethodManager)
12.titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
13.if(isFocus)
14.{
15.imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
16.}
17.else
18.{
19.imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0);
20.}
21.}
22.}, 100);
23.}
EditText titleInput = (EditText) findViewById(R.id.create_edit_title);titleInput.setFocusable(true);
titleInput.requestFocus();
onFocusChange(titleInput.isFocused());
private void onFocusChange(boolean hasFocus)
{
final boolean isFocus = hasFocus;
(new Handler()).postDelayed(new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager)
titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(isFocus)
{
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
else
{
imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0);
}
}
}, 100);
}
我覺得因為在Android的主線程中對UI的操作無效,所以必須在Handler中實現彈出輸入法的操作
三。關於焦點和輸入法的個人理解
獲取焦點是獲取焦點,彈輸入法是彈輸入法。獲取焦點後並不一定會彈出輸入法,在網上搜了一圈,主流回答是「還有就是已開啟界面就是focus的text的話有可能也是不行的,UI渲染是需要時間的」......
由於對源碼不懂,我對這一點也沒有個全面的認識。只能將焦點和輸入法分成兩塊來處理。焦點的打開和關閉特別簡單。
焦點的獲取:
titleInput.setFocusable(true);
titleInput.requestFocus();
焦點的取消:
titleInput.setFocusable(false);四。關於經常調用的處理軟鍵盤的函數如下:<轉載>
1、打開輸入法窗口:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 接受軟鍵盤輸入的編輯文本或其它視圖
imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
2、關閉出入法窗口
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//接受軟鍵盤輸入的編輯文本或其它視圖
inputMethodManager
.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
3、如果輸入法打開則關閉,如果沒打開則打開
InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
4、獲取輸入法打開的狀態
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen若返回true,則表示輸入法打開
轉載
❷ 如何使edittext失去焦點
EditText初始化時候失去焦點:只需要在布局文件中設置屬性
android:focusable="false"
也可以在代碼中由開發者根據需求設置一定的條件,當條件滿足後,動態的設置EditText失去焦點。
示例代碼:
EditTextet=(EditText)findViewById(R.id.et);
et.clearFocus();
et.setFocusable(false);
這種控制EditText的操作是Android程序中不經常用到的。多出現於EditText的輸入監聽回調方法中。
❸ Android中EditText獲得焦點後馬上又失去了,可能是什麼造成的呢
中EditText獲得焦點後馬上又失去了有以下幾種情況:
EditText處在一個可以滾動的控制項中,例如ListView等,當得到焦點後軟體鍵盤會彈出,這個時候滾動控制項會重繪,因此會失去焦點。
EditText注冊了焦點事件,得到焦點後將焦點轉向了其它控制項。
EditText禁止了獲取焦點。
❹ android listview edittext 焦點 滾動
曾經做過這個,但是如果要我敲出代碼給你,這個需要比較多的精力。
提供個思路給你:
你可以獲取到當前Activity下面的所有View,並且你能判斷出Edittext有哪些,還有就是當前Edittext下面的那個Edittext是哪個,這樣的話,你按回車 直接在TextWatcher中的onTextChanged中做好處理,當用戶按了回車,你就直接讓焦點跑到下一個EditText上。
❺ android 自定義的dialog,edit text 不能獲得焦點,彈出軟鍵盤。
1、首先自定義布局
2、把自定義布局放入dialog中顯示
3、通過自定義布局查找對應的edittext組件
final View DialogView = a .inflate ( R.layout.loand, null);//1、自定義布局
//創建對話框
AlertDialog dlg = new AlertDialog.Builder(loand.this)
.setTitle("登錄框")
.setView(DialogView)//設置自定義對話框的樣式,2、自定義布局放入dialog中顯示
.setPositiveButton("登陸", //設置"確定"按鈕
new DialogInterface.OnClickListener() //設置事件監聽{
public void onClick(DialogInterface dialog, int whichButton){editText1 =(EditText) DialogView.findViewById(R.id.editText1);
editText2 =(EditText) DialogView.findViewById(R.id.editText2);//3、過自定義布局查找對應的edittext組件
String id = editText1.getText().toString();
String password = editText2.getText().toString();