问题

I have an array of editTexts which I make like this:

inputs[i] = new EditText(this);

inputs[i].setWidth(376);

inputs[i].setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

tFields.addView(inputs[i]);

I want every character to be capitalized. Right now, the first letter of the first word is lowercase, then all the characters afterwords are upper case. I can take what the user inputs after they are done and convert that to uppercase, but that's not really what I'm going for. Is there a way to get these fields to behave the way I want them to?

回答1:

You need to tell it it's from the "class" text as well:

inputs[i] = new EditText(this);

inputs[i].setWidth(376);

inputs[i].setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

tFields.addView(inputs[i]);

The input type is a bitmask. You can combine the flags by putting the | (pipe) character in the middle, which stands for the OR logic function, even though when used in a bitmask like this it means "this flag AND that other flag".

(This answer is the same as Robin's but without "magic numbers", one of the worst things you can put in your code. The Android API has constants, use them instead of copying the values and risking to eventually break the code.)

回答2:

This will make all the characters uppercase when writing.

edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

Original answer here.

回答3:

I know this question is a bit old but here is explained how to do it in different ways:

Applying UpperCase in XML

Add the following to the EditText XML:

android:inputType="textCapCharacters"

Applying UpperCase as the only filter to an EditText in Java

Here we are setting the UpperCase filter as the only filter of the EditText. Notice that this method removes all the previously added filters.

editText.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

Adding UpperCase to the existing filters of an EditText in Java

To keep the already applied filters of the EditText, let's say inputType, maxLength, etc, you need to retrieve the applied filters, add the UpperCase filter to those filters, and set them back to the EditText. Here is an example how:

InputFilter[] editFilters = editText.getFilters();

InputFilter[] newFilters = new InputFilter[editFilters.length + 1];

System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);

newFilters[editFilters.length] = new InputFilter.AllCaps();

editText.setFilters(newFilters);

回答4:

Just use String.toUpperCase() method.

example :

String str = "blablabla";

editText.setText(str.toUpperCase()); // it will give : BLABLABLA

EDIT :

add this attribute to you EditText Tag in your layout xml :

android:textAllCaps="true"

OR:

If you want whatever the user types to be uppercase you could implement a TextWatcher and use the EditText addTextChangedListener to add it, an on the onTextChange method take the user input and replace it with the same text in uppercase.

editText.addTextChangedListener(upperCaseTextWatcher);

final TextWatcher upperCaseTextWatcher = new TextWatcher() {

public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

editText.setText(editText.getText().toString().toUpperCase());

editText.setSelection(editText.getText().toString().length());

}

public void afterTextChanged(Editable editable) {

}

};

回答5:

You can try this:

android:inputType="textCapCharacters"

It worked for me. =)

回答6:

To capitalize the first word of each sentence use :

android:inputType="textCapSentences"

To Capitalize The First Letter Of Every Word use :

android:inputType="textCapWords"

To Capitalize every Character use :

android:inputType="textCapCharacters"

回答7:

This would work

inputs[i] = new EditText(this);

inputs[i].setWidth(376);

inputs[i].setInputType(0x00001001);

tFields.addView(inputs[i]);

0x00001001 corresponds to "textCapCharacters constant.

回答8:

As of today, Its

android:inputType="textCapCharacters"

回答9:

try using this single line of code in your xml:

android:capitalize="sentences"

回答10:

For some reason adding the xml did not work on my device.

I found that a Text Filter works for to uppercase, like this

EditText editText = (EditText) findViewById(R.id.editTextId);

InputFilter toUpperCaseFilter = new InputFilter() {

public CharSequence filter(CharSequence source, int start, int end,

Spanned dest, int dstart, int dend) {

StringBuilder stringBuilder = new StringBuilder();

for (int i = start; i < end; i++) {

Character character = source.charAt(i);

character = Character.toUpperCase(character); // THIS IS UPPER CASING

stringBuilder.append(character);

}

return stringBuilder.toString();

}

};

editText.setFilters(new InputFilter[] { toUpperCaseFilter });

来源:https://stackoverflow.com/questions/13705776/how-to-capitalize-every-letter-in-an-android-edittext

android capitalize,How to capitalize every letter in an Android EditText?相关推荐

  1. android什么控件能够输入多行文字,Android开发:文本控件详解——EditText(一)基本属性...

    一.简单实例: EditText输入的文字样式部分的属性,基本都是和TextView中的属性一样. 除此之外,EditText还有自己独有的属性. 二.基本属性: hint 输入框显示的提示文本 te ...

  2. This version of Android Studio cannot open this project, please retry with Android Studio 3.5 or new

    今天github 下载一个库 导入 as 提示 This version of Android Studio cannot open this project, please retry with A ...

  3. android组件什么时候加载到r文件,Android自定义加载loading view动画组件

    我写写使用步骤 自定义view(CircleProgress )的代码 package com.hysmarthotel.view; import com.hysmarthotel.roomcontr ...

  4. 《ArcGIS Runtime SDK for Android开发笔记》——(6)、基于Android Studio的ArcGIS Android工程结构解析...

    1.前言 Android Studio 是第一个Google官方的 Android 开发环境.其他工具,例如 Eclipse,在 Android Studio 发布之前已经有了大规模的使用.为了帮助开 ...

  5. Android提前加载unity程序,Unity项目嵌入Android App实现过程

    1.编写自己的Unity APP,完成后,导出为Android 工程(参数和步骤截图如下),unity版本为:2019.4.16 2.导出的文件夹内容如下截图:我们只使用unityLibrary这个文 ...

  6. Android studio 使用心得(三)—从Eclipse迁移到Android studio

    断断续续的也算是把eclipse上的代码成功迁移到android studio上来了,现在,我同事继续用eclipse,我用android studio,svn上还是之前eclipse的项目,迁移成功 ...

  7. android5.1和ios差距,Android 5.1和IOS运行流畅度比较Android获胜!

    实践是检验真相的唯一标准,它一直是发布它的人们的教育,所以我一直认为Android不会比ios更加流畅,但是由于我吃了苹果,所以我有了改变了我以前的看法. 它是ip6p,系统是ios8.4,比较And ...

  8. android studio visual studio 2015,Visual Studio Emulator for Android

    Visual Studio Emulator for Android 11/15/2016 4 分钟可看完 本文内容 Note This article applies to Visual Studi ...

  9. android activity启动流程_1307页!一线大厂Android面试全套真题解析!

    /   前言   / 金九银十到了,很多读者都反映有面试的需求,所以我特地给大家准备了一点资料! 下面的题目都是大家在面试一线互联网大厂时经常遇到的面试真题和答案解析,如果大家还有其他好的题目或者好的 ...

最新文章

  1. shell命令之(一) 初探grep
  2. python中module_Python中的模块(Module)
  3. 概念介绍(机器学习)
  4. 学习手记(2021/3/19~?)
  5. Mac OS开发—Xcode给Mac应用添加编辑快捷键(剪切 复制 粘贴 全选 删除 撤销 重做)功能
  6. A3 没有装入任何送纸器
  7. 3d激光雷达开发(从halcon看点云pcl库)
  8. nodejs入门开发与常用模块
  9. LabVIEW安装多个NI软件产品时的安装顺序
  10. ubuntu前置耳机孔没声音的解决办法
  11. 内室设计软件测试,室内量房APP哪家强?5款软件深度测评(量房易用性篇)
  12. W10如何下载经典扫雷游戏
  13. NLP学习(七)使用stanford实现句法分析-Python3实现
  14. 基于 Verilog 的经典数字电路设计(10)三态门
  15. 把uTorrent做成绿色版
  16. 【Linux】无法读取/挂载U盘
  17. 实验一 熟悉常用的Linux操作和Hadoop操作
  18. 乳牛悲惨的一生 -----奶牛为什么会产奶?
  19. easyuefi如何添加引导_easyuefi怎么用?easyuefi基本使用方法介绍
  20. HiBench算法简介

热门文章

  1. 安卓学习笔记17:常用控件 - 编辑框
  2. 《天天数学》连载00:序言
  3. PHP开发套件采用wamp时配置PHPStorm
  4. 网络计算机热词,2017年的首个网络热词就这样被刷屏了!
  5. 和电商有关的词语_电商描写的词语 形容“电”的词语有哪些?
  6. java Servlet技术·笔记
  7. linux入门 适合初学者_【推荐】适合初学者临摹的国画|国画基础入门教学视频教程!...
  8. chmod简介及其使用方法
  9. 查看、修改linux系统的最大链接数限制、文件描述符限制、端口范围限制、虚拟内存等
  10. 几点关于C/C++开发的思考