android隐藏edittext
A. android开发中,EditText点击事件可以响应,就是不弹出软键盘。怎么解决,EditText是在Dialog中使用的
当点击EditText时:
1、存储当前的输入类型
2、设置输入类型为InputType.TYPE_NULL,禁止手机软键盘
3、还原原来的输入类型,因为这次操作完,还要允许用户修改。
4、弹出日期选择对话框,操作完后将结果显示到EditText上。
隐藏软件盘,代码如下:
EditText et_start_time = (EditText) this.findViewById(R.id.et_start_time);
int inType = et_start_time.getInputType();
et_start_time.setInputType(InputType.TYPE_NULL);
et_start_time.onTouchEvent(event);
et_start_time.setInputType(inType);
et_start_time.setSelection(et_start_time.getText().length());
B. android 怎样edittext 键盘失去焦点时自动关闭
android 怎样edittext 键盘失去焦点时自动关闭
软键盘的原理
软键盘其实是一个Dialog。InputMethodService为我们的输入法创建了一个Dialog,并且对某些参数进行了设置,使之能够在底部或者全屏显示。当我们点击输入框时,系统会对当前的主窗口进行调整,以便留出相应的空间来显示该Dialog在底部,或者全屏。
2.活动主窗口调整
Android定义了一个属性windowSoftInputMode,
用它可以让程序控制活动主窗口调整的方式。我们可以在配置文件AndroidManifet.xml中对Activity进行设置。这个属性的设置将会影响两件事情:
1>软键盘的状态——隐藏或显示。
2>活动的主窗口调整——是否减少活动主窗口大小以便腾出空间放软键盘或是否当活动窗口的部分被软键盘覆盖时它的内容的当前焦点是可见的。
故该属性的设置必须是下面列表中的一个值,或一个“state…”值加一个“adjust…”值的组合。在任一组设置多个值,各个值之间用|分开。
"stateUnspecified":The state of the soft keyboard (whether it is hidden or
visible) is not specified. The system will choose an appropriate state or rely
on the setting in the theme. This is the default setting for the behavior of the
soft keyboard. 软键盘的状态(隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设置。这个是软件盘行为的默认设置。
"stateUnchanged":The soft keyboard is kept in whatever state it was last
in, whether visible or hidden, when the activity comes to the fore.
软键盘被保持上次的状态。
"stateHidden":The soft keyboard is hidden when the user chooses the
activity — that is, when the user affirmatively navigates forward to the
activity, rather than backs into it because of leaving another activity.
当用户选择该Activity时,软键盘被隐藏。
"stateAlwaysHidden":The soft keyboard is always hidden when the activity's
main window has input focus. 软键盘总是被隐藏的。
"stateVisible":The soft keyboard is visible when that's normally
appropriate (when the user is navigating forward to the activity's main window).
软键盘是可见的。
"stateAlwaysVisible":The soft keyboard is made visible when the user
chooses the activity — that is, when the user affirmatively navigates forward to
the activity, rather than backs into it because of leaving another activity.
当用户选择这个Activity时,软键盘是可见的。
"adjustUnspecified":It is unspecified whether the activity's main window
resizes to make room for the soft keyboard, or whether the contents of the
window pan to make the currentfocus visible on-screen. The system will
automatically select one of these modes depending on whether the content of the
window has any layout views that can scroll their contents. If there is such a
view, the window will be resized, on the assumption that scrolling can make all
of the window's contents visible within a smaller area. This is the default
setting for the behavior of the main window.
它不被指定是否该Activity主窗口调整大小以便留出软键盘的空间,或是否窗口上的内容得到屏幕上当前的焦点是可见的。系统将自动选择这些模式中一种主要依赖于是否窗口的内容有任何布局视图能够滚动他们的内容。如果有这样的一个视图,这个窗口将调整大小,这样的假设可以使滚动窗口的内容在一个较小的区域中可见的。这个是主窗口默认的行为设置。也就是说,系统自动决定是采用平移模式还是压缩模式,决定因素在于内容是否可以滚动。
"adjustResize":(压缩模式)The activity's main window is always resized to make
room for the soft keyboard on screen. 当软键盘弹出时,要对主窗口调整屏幕的大小以便留出软键盘的空间。
"adjustPan":(平移模式:当输入框不会被遮挡时,该模式没有对布局进行调整,然而当输入框将要被遮挡时,窗口就会进行平移。也就是说,该模式始终是保持输入框为可见。)The
activity's main window is not resized to make room for the soft keyboard.
Rather, the contents of the window are automatically panned so that the current
focus is never obscured by the keyboard and users can always see what they are
typing. This is generally less desirable than resizing, because the user may
need to close the soft keyboard to get at and interact with obscured parts of
the window.
该Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作。。
3.侦听软键盘的显示隐藏
有时候,借助系统本身的机制来实现主窗口的调整并非我们想要的结果,我们可能希望在软键盘显示隐藏的时候,手动的对布局进行修改,以便使软键盘弹出时更加美观。这时就需要对软键盘的显示隐藏进行侦听。
我们可以借助软键盘显示和隐藏时,对主窗口进行了重新布局这个特性来进行侦听。如果我们设置的模式为压缩模式,那么我们可以对布局的onSizeChanged函数进行跟踪,如果为平移模式,那么该函数可能不会被调用。
4.EditText默认不弹出软件键盘
方法一:
在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden
例如:
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified|stateHidden"
android:configChanges="orientation|keyboardHidden">
方法二:
让EditText失去焦点,使用EditText的clearFocus方法
例如:EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
强制隐藏Android输入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
5.EditText始终不弹出软件键盘
例:EditText edit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
C. android 后面代码怎么设置edittext的值
1、EditText输入的文字为密码形式的设置
(1)通过.xml里设置:
把该EditText设为:android:password="true" // 以”.”形式显示文本
(2)在代码里设置:
通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码。
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置密码为不可见。
2、(1)EditText输入的文字为电话号码
Android:phoneNumber=”true” //输入电话号码
3、EditText字数限制的设置
(1)在.xml中设置:android:maxLength=“50”
(2)代码中设置:
editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});
4、EditText设置字体
android:typeface="monospace" //设置字型。字形有:normal, sans, serif,monospace
5、EditText是否可编辑
Android:editable // 是否可编辑
6、在EditText中软键盘的调起、关闭
(1)EditText有焦点(focusable为true)阻止输入法弹出
editText=(EditText)findViewById(R.id.txtBody);
editText.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
editText.setInputType(InputType.TYPE_NULL); //关闭软键盘
return false;
}
});
(2)当EidtText无焦点(focusable=false)时阻止输入法弹出
InputMethodManager imm =
(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(),0);
(3)调用数字键盘并设置输入类型和键盘为英文
etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //调用数字键盘
rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//设置输入类型和键盘为英文 或者:android:inputType="textUri|textMultiLine"
(4)android:focusable="false"//键盘永远不会弹出
<activity android:name=".AddLinkman"android:windowSoftInputMode="adjustUnspecified|stateHidden"/>//不自动弹出键盘
//关闭键盘(比如输入结束后执行) InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(etEditText.getWindowToken(), 0);
//自动弹出键盘
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
etEditText.requestFocus();//让EditText获得焦点,但是获得焦点并不会自动弹出键盘
7、android:layout_gravity和android:gravity的区别
(1)android:layout_gravity是本元素对父元素的重力方向。
(2)android:gravity是本元素所有子元素的重力方向。
8、android:padding和android:layout_margin区别
这两个都可以设置边距,但有细微的区别:
(1)android:padding是相对父view的边距
(2)android:layout_margin是相对同一级View的边距
例:LinearLayout是水平布局,下面有两个按钮,
(a)如果右边的按钮想距左边的按钮15px,因为这两个按钮是同一级的,应该用android:layout_margin;
(b)如果右边的按钮想距左边的距离为350px,应该用android:padding
9、android:numeric//只接受数字
android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数,有正负)和decimal(浮点数)。
D. Android设置EditText默认取消焦点
在Android的布局文见中,只要存在EditText,当显示此布局的时候,EditText就会默认获取焦点并打开软键盘。
在EditText的父控件中,添加两个属性即可:
添加完成后的写法如下所示:
经试验,在EditText标签中添加这两行只会让软键盘隐藏,但是光标默认还是在编辑框中闪烁。
按照上文中的写法可以默认隐藏光标。
E. android editText 输入法把选项卡顶上去了怎么办
在清单文件AndroidManifest.xml中修改acitivty的windowInputMode为adjustPan即可
windowInputMode有以下几个取值
1.stateUnspecified
中文意思是未指定状态,当我们没有设置android:windowSoftInputMode属性的时候,软件默认采用的就是这种交互方式,系统会根据界面采取相应的软键盘的显示模式,比如,当界面上只有文本和按钮的时候,软键盘就不会自动弹出,因为没有输入的必要。
2.stateUnchanged
中文的意思就是状态不改变的意思,我们应该怎么理解这句话呢?其实很好理解,就是说,当前界面的软键盘状态,取决于上一个界面的软键盘状态。假如当前界面键盘是隐藏的,那么跳转之后的界面,软键盘也是隐藏的;如果当前界面是显示的,那么跳转之后的界面,软键盘也是显示状态。
3.stateHidden
顾名思义,如果我们设置了这个属性,那么键盘状态一定是隐藏的,不管上个界面什么状态,也不管当前界面有没有输入的需求,反正就是不显示。因此,我们可以设置这个属性,来控制软键盘不自动的弹出。
4.stateAlwaysHidden
这个属性也可以让软键盘隐藏
5.stateVisible
设置为这个属性,可以将软键盘召唤出来,即使在界面上没有输入框的情况下也可以强制出来。
6.stateAlwaysVisible
这个属性也是可以将键盘召唤出来,但是与stateVisible属性有小小的不同之处。举个例子,当我们设置为stateVisible属性,如果当前的界面键盘是显示的,当我们点击按钮跳转到下个界面的时候,软键盘会因为输入框失去焦点而隐藏起来,当我们再次回到当前界面的时候,键盘这个时候是隐藏的。但是如果我们设置为stateAlwaysVisible,我们跳转到下个界面,软键盘还是隐藏的,但是当我们再次回来的时候,软键盘是会显示出来的。所以,这个Always就解释了这个区别,不管什么情况到达当前界面(正常跳转或者是上一个界面被用户返回),软键盘都是显示状态。
7.adjustUnspecified
从这个属性开始,就不是设置软键盘的显示与隐藏模式了,而是设置软键盘与软件的显示内容之间的显示关系。当没有设置这个值的时候,这个选项也是默认的设置模式。在这中情况下,系统会根据界面选择不同的模式。如果界面里面有可以滚动的控件,比如ScrowView,系统会减小可以滚动的界面的大小,从而保证即使软键盘显示出来了,也能够看到所有的内容。如果布局里面没有滚动的控件,那么软键盘可能就会盖住一些内容,
8.adjustResize
这个属性表示Activity的主窗口总是会被调整大小,从而保证软键盘显示空间。
9.adjustPan
如果设置为这个属性,那么Activity的屏幕大小并不会调整来保证软键盘的空间,而是采取了另外一种策略,系统会通过布局的移动,来保证用户要进行输入的输入框肯定在用户的失业范围里面,从而让用户可以看到自己输入的内容。