android 如何点击按键 让edittext 获取焦点

android 如何点击按键 让edittext 获取焦点,第1张

edittextsetFocusable(true);

edittextsetFocusableInTouchMode(true);

edittextrequestFocus();

edittextfindFocus();

InputMethodManager inputManager = (InputMethodManager)edittextgetContext()getSystemService(ContextINPUT_METHOD_SERVICE);

inputManagershowSoftInput(edittext, 0);

试试这个看好用不。

EditTextsetOnFocusChangeListener(new ViewOnFocusChangeListener() {  

      

    @Override  

    public void onFocusChange(View v, boolean hasFocus) {  

        if(hasFocus){//获得焦点  

              

        }else{//失去焦点  

            

        }  

    }             

});

一:EditText为什么会默认d出输入法

同样的代码,碰到有EditText控件的界面时有的机子会d出输入法,有的机子不会d出。不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研究android源码时再搞个清楚。但是我有解决方案。

二:默认d出和默认关闭输入法的解决方案。

1默认关闭,不至于进入Activity就打开输入法,影响界面美观。

①在布局文件中,在EditText前面放置一个看不到的LinearLayout,让他率先获取焦点:

[html] view plaincopyprint

01<LinearLayout

02

03android:focusable="true"

04

05android:focusableInTouchMode="true"

06

07android:layout_width="0px"

08

09android:layout_height="0px"/>

<LinearLayout

android:focusable="true"

android:focusableInTouchMode="true"

android:layout_width="0px"

android:layout_height="0px"/>

②方法二:先看一个属性android:inputType:指定输入法的类型,int类型,可以用|选择多个。取值可以参考:androidtextInputType类。取值包括: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,输入法就不会d出然后再设置监听,再重新设置它的InputType

[java] view plaincopyprint

01editTextsetOnTouchListener(new OnTouchListener() {

02 public boolean onTouch(View v, MotionEvent event) {

03 // TODO Auto-generated method stub

04 int inType = editTextgetInputType(); // backup the input type

05 editTextsetInputType(InputTypeTYPE_NULL); // disable soft input

06 editTextonTouchEvent(event); // call native handler

07 editTextsetInputType(inType); // restore input type

08 return true;

09 }

10 });

editTextsetOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

int inType = editTextgetInputType(); // backup the input type

editTextsetInputType(InputTypeTYPE_NULL); // disable soft input

editTextonTouchEvent(event); // call native handler

editTextsetInputType(inType); // restore input type

return true;

}

});

2默认d出。有时候按照需求可能要求默认d出输入法。方案如下:

[java] view plaincopyprint

01EditText titleInput = (EditText) findViewById(Ridcreate_edit_title);titleInputsetFocusable(true);

02

03 titleInputrequestFocus();

04 onFocusChange(titleInputisFocused());

05

06private void onFocusChange(boolean hasFocus)

07{

08final boolean isFocus = hasFocus;

09(new Handler())postDelayed(new Runnable() {

10public void run() {

11InputMethodManager imm = (InputMethodManager)

12titleInputgetContext()getSystemService(ContextINPUT_METHOD_SERVICE);

13if(isFocus)

14{

15immtoggleSoftInput(0, InputMethodManagerHIDE_NOT_ALWAYS);

16}

17else

18{

19immhideSoftInputFromWindow(titleInputgetWindowToken(),0);

20}

21}

22}, 100);

23}

EditText titleInput = (EditText) findViewById(Ridcreate_edit_title);titleInputsetFocusable(true);

 titleInputrequestFocus();

onFocusChange(titleInputisFocused());

private void onFocusChange(boolean hasFocus)

{

final boolean isFocus = hasFocus;

(new Handler())postDelayed(new Runnable() {

public void run() {

InputMethodManager imm = (InputMethodManager)

titleInputgetContext()getSystemService(ContextINPUT_METHOD_SERVICE);

if(isFocus)

{

immtoggleSoftInput(0, InputMethodManagerHIDE_NOT_ALWAYS);

}

else

{

immhideSoftInputFromWindow(titleInputgetWindowToken(),0);

}

}

}, 100);

}

我觉得因为在Android的主线程中对UI的 *** 作无效,所以必须在Handler中实现d出输入法的 *** 作

三。关于焦点和输入法的个人理解

获取焦点是获取焦点,d输入法是d输入法。获取焦点后并不一定会d出输入法,在网上搜了一圈,主流回答是“还有就是已开启界面就是focus的text的话有可能也是不行的,UI渲染是需要时间的”

由于对源码不懂,我对这一点也没有个全面的认识。只能将焦点和输入法分成两块来处理。焦点的打开和关闭特别简单。

焦点的获取:

titleInputsetFocusable(true);

titleInputrequestFocus();

焦点的取消:

titleInputsetFocusable(false); 四。关于经常调用的处理软键盘的函数如下:<转载>

1、打开输入法窗口:

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(ContextINPUT_METHOD_SERVICE);

// 接受软键盘输入的编辑文本或其它视图

immshowSoftInput(submitBt,InputMethodManagerSHOW_FORCED);

2、关闭出入法窗口

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(ContextINPUT_METHOD_SERVICE);

inputMethodManagerhideSoftInputFromWindow(OpeListActivitythisgetCurrentFocus()getWindowToken(),

InputMethodManagerHIDE_NOT_ALWAYS);

//接受软键盘输入的编辑文本或其它视图

inputMethodManager

showSoftInput(submitBt,InputMethodManagerSHOW_FORCED);

3、如果输入法打开则关闭,如果没打开则打开

InputMethodManager m=(InputMethodManager) getSystemService(ContextINPUT_METHOD_SERVICE);

mtoggleSoftInput(0, InputMethodManagerHIDE_NOT_ALWAYS);

4、获取输入法打开的状态

InputMethodManager imm = (InputMethodManager)getSystemService(ContextINPUT_METHOD_SERVICE);

boolean isOpen=immisActive();

isOpen若返回true,则表示输入法打开

转载

这三个属性必须同时设置:

private EditText passwde = ;

passwdesetFocusable(true);

passwdesetFocusableInTouchMode(true);

passwderequestFocus();

悬浮窗不支持输入的,本身有很多限制的,还有不能全屏等等,很多,如果要实现输入,最好还是开一个activity,哪怕是透明的也行,一般的做法就是点击输入框直接d出半透明页面,在这里面的输入框中输入

EditText初始化时候失去焦点:只需要在布局文件中设置属性

1

android:focusable="false"

也可以在代码中由开发者根据需求设置一定的条件,当条件满足后,动态的设置EditText失去焦点。

示例代码:

1

2

3

EditText et = (EditText) findViewById(Ridet);

etclearFocus();

etsetFocusable(false);

这种控制EditText的 *** 作是Android程序中不经常用到的。多出现于EditText的输入监听回调方法中。

以上就是关于android 如何点击按键 让edittext 获取焦点全部的内容,包括:android 如何点击按键 让edittext 获取焦点、android 怎么在代码中判断edittext有没有获取焦点、android中edittex焦点设置和d不d出输入法的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9620999.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存