EditText 默认不弹键盘 焦点

 今天编程碰到了一个问题:有一款平板,打开一个有EditText的Activity会默认弹出输入法。为了解决这个问题就深入研究了下android中焦点Focus和弹出输入法的问题。在网上看了些例子都不够全面,在这里全面总结下。

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

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

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

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

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

  <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

  先将EditText的InputType改变为TYPE_NULL,输入法就不会弹出.然后再设置监听,再重新设置它的InputType.

  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.默认弹出。有时候按照需求可能要求默认弹出输入法。方案如下:  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,则表示输入法打开

时间: 2024-08-03 03:09:55

EditText 默认不弹键盘 焦点的相关文章

EditText默认不弹出软键盘

#EditText默认不弹出软键盘# 网上关于EditText默认情况下不弹出软键盘,当手触摸到EditText,获取焦点时候,才会弹出软键盘,貌似都不能用,其实,在oncreate()方法中,加上 getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 就能达到预期效果,至于,让EditText永远不获取焦点的方法,网上大把的,百度即可,暂时没有遇见让EditText永远不获取焦点的场

分享:EditText默认不弹出软件键盘

方法一: 在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden 例如: <activityandroid:name=".Main"android:label="@string/app_name"android:windowSoftInputMode="adjustUnspecified|stateHidden"andro

WPF设置控件获取键盘焦点时的样式FocusVisualStyle

控件获取焦点除了用鼠标外,可以通过键盘来获取,比如Tab键或者方向键等,需要设置控件获取键盘焦点时的样式,可以通过设置FrameworkElemnt.FocusVisualStyle属性, 因为几乎所有常用的控件都继承了FrameworkElement,所以绝大部分控件都拥有该属性 // Summary: // Gets or sets a property that enables customization of appearance, effects, // or other style

EditText 默认不获取焦点,弹出软键盘布局变形解决方案

关于弹出软键盘布局变形解决方案: 在androidMainfest.xml文件中在此Activity中写入 android:windowSoftInputMode="adjustPan" 在实际开发中,有的页面用到Edittext控件,这时候进入该页面可能会自动弹出输入法 这么显示不太友好,所以需要设置一下让Edittext默认不自动获取焦点.在网上查资料解决办法如下: 在EditText的父级控件中找一个,设置成 android:focusable="true"

android的alertdialog中加入edittext但是不弹出软键盘等问题的解决与原因

摘要:alertdialog中加入edittext但是不弹出软键盘等问题网上有很多不管用的解决方案, 本文意在给出更有效的解决办法,并初步探究其原因 正文 在对话框中插入文本框是十分常见的需求 通常我们选择在代码中创建edittext对象 这个时候就需要在代码中给edittext设置输入属性了 但是经常发现设置的属性不起作用,甚至都不弹出软键盘(虽然实体键盘可以输入) 问题的解决方案有很多种,这里介绍一种我比较常用的方法: inputPassEditText.setInputType(Input

Android EditText默认不获得焦点

页面上有EditText默认获得了光标,如果想让EditText默认不获得焦点,可以在控件前面加一个隐藏的线性布局,让它先获得焦点 <LinearLayout     android:focusable="true"     android:focusableInTouchMode="true"     android:layout_width="0px"     android:layout_height="0px"

edittext禁止android软键盘弹出

1. EditText ed=(EditText) findViewById(R.id.test); ed.clearFocus(); 2. 在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden 比如:<activity android:name=".Main" android:label="@string/app_name" androi

Android开发,在Activity启动时,默认隐藏软键盘。和遮挡Edittext时的处理

在Activity启动时,默认隐藏软键盘: 在AndroidManifest.xml中找到你得Activity ,为它添加属性: android:windowSoftInputMode="stateAlwaysHidden" ------------------------ 输入遮挡Edittext时的处理: 在AndroidManifest.xml中找到你得Activity ,为它添加属性: android:windowSoftInputMode="adjustPan&qu

IsKeyboardFocused -- 键盘焦点

1 <Trigger Property="IsKeyboardFocused" Value="true"> 2 <!--<Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>--> 3 <Setter Property="BorderBrush" TargetNa