android EditText焦点问题

android EditText焦点问题,第1张

根据你的描述,思路如下:

首先你要有数据存放,以便首次记录EditText1的数据后,再次登录能知晓EditText1已经有输入值,以此作判定是否给EditText1焦点

数据 *** 作有很多,无论是用数据库还是xml都行。

一: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,则表示输入法打开

转载

EditTextsetOnFocusChangeListener(new ViewOnFocusChangeListener() {  

      

    @Override  

    public void onFocusChange(View v, boolean hasFocus) {  

        if(hasFocus){//获得焦点  

              

        }else{//失去焦点  

            

        }  

    }             

});

如何让 EditText 在 Activity 启动时不获得焦点

在进入一个Activity时,如果这个Activity中有EditText,则这个EditText会自动获取焦点,然后就会d出软键盘,这样给用户体验不是很好。所以一般会通过代码控制让EditText不获取焦点。常用的方式如下,在Activity的布局文件中加上如下代码:

<!-- 输入焦点控制 -->

<LinearLayout

android:layout_width="0px"

android:layout_height="0px"

android:focusable="true"

android:focusableInTouchMode="true" />

在日前开发的一个项目中界面中除了EditText就是Spinner 和Button等空间,EditText 获取焦点以后,点击Spinner总是无法移除焦点:采取如下方式解决,

当点击Spinner时让EditText失去焦点即可:(见标红代码)

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存