温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现Android键盘的中英文适配

发布时间:2021-08-06 10:59:42 来源:亿速云 阅读:134 作者:小新 栏目:移动开发

这篇文章将为大家详细讲解有关如何实现Android键盘的中英文适配,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

英文环境下,密码框字体和一般字体不一致问题

1、xml中不能设置inputType 属性、或者password属性

2、中文环境中设置inputType可以

3、当要是适配英文,只能在Java代码设置

如何实现Android键盘的中英文适配

android开发EditText输入时弹出数字输入键盘(适配英文环境)

首先设置只能输入数字

<EditText
android:id="@+id/second_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890"
android:maxLength="6"
android:paddingLeft="@dimen/dp_20"
android:singleLine="true"
android:textSize="@dimen/sp_14"/>

重点是

android:digits="1234567890"

EditText中android:digits属性的作用

是设置允许输入哪些字符。如“1234567890.+-*/%\n()”

再在代码里面设置输入法类型:

secondPassword.setInputType(EditorInfo.TYPE_CLASS_PHONE);//数字键盘
secondPassword.setTransformationMethod(new PasswordTransformationMethod());//密文

则如果该EditText获得焦点,会弹出数字输入法的模拟键盘

请在xml中设置inputType属性即可

1、API中有,列举出来inputType的值都包括哪些。

android:inputType=”none”
android:inputType=”text”
android:inputType=”textCapCharacters” 字母大写
android:inputType=”textCapWords” 首字母大写
android:inputType=”textCapSentences” 仅第一个字母大写
android:inputType=”textAutoCorrect” 自动完成
android:inputType=”textAutoComplete” 自动完成
android:inputType=”textMultiLine” 多行输入
android:inputType=”textImeMultiLine” 输入法多行(如果支持)
android:inputType=”textNoSuggestions” 不提示
android:inputType=”textUri” 网址
android:inputType=”textEmailAddress” 电子邮件地址
android:inputType=”textEmailSubject” 邮件主题
android:inputType=”textShortMessage” 短讯
android:inputType=”textLongMessage” 长信息
android:inputType=”textPersonName” 人名
android:inputType=”textPostalAddress” 地址
android:inputType=”textPassword” 密码
android:inputType=”textVisiblePassword” 可见密码
android:inputType=”textWebEditText” 作为网页表单的文本
android:inputType=”textFilter” 文本筛选过滤
android:inputType=”textPhonetic” 拼音输入
//数值类型
android:inputType=”number” 数字
android:inputType=”numberSigned” 带符号数字格式
android:inputType=”numberDecimal” 带小数点的浮点格式
android:inputType=”phone” 拨号键盘
android:inputType=”datetime” 时间日期
android:inputType=”date” 日期键盘
android:inputType=”time” 时间键盘

2、Enter键图标的设置

想象一下,当我们在EditText中完成了输入,想要以输入的内容作为关键字进行搜索时,却需要按下“完成”图标的Enter按键,显然这不符合良好的用户体验设计。 那么,怎么样来改变Enter按键的图标呢?

Android为我们提供了android:imeOptions来实现这一功能。
android:imeOptions的常用参数有以下一些:

normal(常规),

actionUnspecified(未指定),

actionNone(没有动作),

actionGo(去往),

actionSearch(搜索),

actionSend(发送),

actionNext(下一个),

actionDone(完成),

flagNoExtractUi,flagNoAccessoryAction,flagNoEnterAction等,其对应的Enter键

图标如图所示:

如何实现Android键盘的中英文适配

3、设置软键盘交互样式

有时键盘弹出需要把界面挤压到上端或直接覆盖界面。 可在AndroidManifest.xml 对应的 Activity 里添加上这条属性:
android:windowSoftInputMode=”参数”

参数详情如下,多个参数之间可用‘|'隔开:

【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置

【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示

【C】stateHidden:用户选择activity时,软键盘总是被隐藏

【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的

【E】stateVisible:软键盘通常是可见的

【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态

【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示

【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间

【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分

EditText默认不弹出软件键盘:

方法一:

在 AndroidMainfest.xml 中选择哪个 activity,设置windowSoftInputMode 属性为 adjustUnspecified|stateHidden
例如:

<activity android:name=".Main"
  android:label="@string/app_name"
  android:windowSoftInputMode="adjustUnspecified|stateHidden"
  android:configChanges="orientation|keyboardHidden">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

方法二:

让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);

4、自动将输入的小写字母转换为大写

自动转化为大写字母。但是转换出来的只是显示为大写字母,存的还是小写字母。

class InputLowerToUpper extends ReplacementTransformationMethod{ 
  @Override 
  protected char[] getOriginal() { 
   char[] lower = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' }; 
   return lower; 
  } 
  @Override 
  protected char[] getReplacement() { 
   char[] upper = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' }; 
   return upper; 
  } 
 } 
editText.setTransformationMethod(new InputLowerToUpper());

也可通过设置 android:inputType=”textCapCharacters”可行,但是就不能一块使用密码键盘了。

//下面这种方法才是真正的将输入的小写字母转换为大写字母
addressText.addTextChangedListener(new TextWatcher() { 
  @Override 
  public void onTextChanged(CharSequence s, int start, int before, int count) { 
   // TODO Auto-generated method stub 
   addressText.removeTextChangedListener(this);//解除文字改变事件 
   addressText.setText(s.toString().toUpperCase());//转换 
   addressText.setSelection(s.toString().length());//重新设置光标位置 
   addressText.addTextChangedListener(this);//重新绑 
//   licensePlateNumber = addressText.getText().toString().trim(); 
  } 
  @Override 
  public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
   // TODO Auto-generated method stub 
  } 
  @Override
  public void afterTextChanged(Editable arg0) {
   // TODO Auto-generated method stub 
  } 
 });

关于“如何实现Android键盘的中英文适配”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI