EditText 的首字母大写
我正在开发一个小型个人待办事项列表应用程序,到目前为止一切都运行良好。我想弄清楚一个小怪癖。每当我要添加新项目时,我都会有一个对话框,其中显示一个 EditText 视图。当我选择 EditText 视图时,键盘会出现,按其应有的方式输入文本。在大多数应用程序中,默认情况似乎是按住第一个字母的 Shift 键......尽管在我看来它并没有这样做。必须有一个简单的方法来修复,但我反复搜索参考文献但找不到它。我认为适配器加载的引用必须有一个 xml 属性,但我不知道它是什么。
I'm working on a little personal todo list app and so far everything has been working quite well. There is one little quirk I'd like to figure out. Whenever I go to add a new item, I have a Dialog with an EditText view showing inside. When I select the EditText view, the keyboard comes up to enter text, as it should. In most applications, the default seems to be that the shift key is held for the first letter... although it does not do this for my view. There has to be a simple way to fix, but I've searched the reference repeatedly and cannot find it. I'm thinking there has to be an xml attribute for the reference loaded by the Adapter, but I can't find out what it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
静态地(即在布局 XML 文件中):在
EditText
上设置android:inputType="textCapSentences"
。以编程方式:您必须在
EditText
的InputType
中包含InputType.TYPE_CLASS_TEXT
,例如- Google 文档
Statically (i.e. in your layout XML file): set
android:inputType="textCapSentences"
on yourEditText
.Programmatically: you have to include
InputType.TYPE_CLASS_TEXT
in theInputType
of theEditText
, e.g.- Google Docs
只需在 EditText 元素中使用
android:inputType="textCapWords"
即可。例如:
参考以下链接:
http://developer.android.com/reference/android/widget/TextView.html# attr_android%3ainputType
Just use
android:inputType="textCapWords"
in your EditText element.For example:
Refer to the following link for reference:
http://developer.android.com/reference/android/widget/TextView.html#attr_android%3ainputType
在 XML 中的 EditText 中应用以下行。
它还将允许多行支持。
Apply following line in your EditText in XML.
It will also allow multi-line support.
或
android:inputType="textCapSentences"
仅在您的设备键盘启用自动大写设置时才有效。or
android:inputType="textCapSentences"
will only work If your device keyboard Auto Capitalize Setting enabled.我遇到了同样的问题,只是分享我发现的。可能对您和其他人有帮助...
在您的布局上尝试一下。在
EditText
中添加以下行。对我来说效果很好..希望它也对你有用...
I encountered the same problem, just sharing what I found out. Might help you and others...
Try this on your layout.add the line below in your
EditText
.works fine on me.. hope it works also on you...
如果您希望每个单词的第一个字母大写,请使用
android:inputType="textCapWords"
为了更好地理解
,如果您希望每个句子的单词大写,则使用它。
android:inputType="textCapSentences"
xml 中的这一行。我的意思是在你的 EditText 中。为了更好的理解
If you want capital first letter in every word then use
android:inputType="textCapWords"
For Better understanding
And if you capital word for every sentence then use it .
android:inputType="textCapSentences"
this line in your xml. I mean in your EditText.For Better understanding
要大写,您可以对编辑文本执行以下操作:
将每个单词的首字母大写:
将每个句子的首字母大写:
将每个字母大写:
但这只会对键盘进行更改,用户可以更改以小写字母书写的模式。
因此,如果您确实想要大写格式的数据,请首先添加以下类:
然后,
现在将每个单词大写:
仅将第一个单词大写:
To capitalize, you can do the following with edit text:
To make first letter capital of every word:
To make first letter capital of every sentence:
To make every letter capital:
But this will only make changes to keyboard and user can change the mode to write letter in small case.
So this approach is not much appreciated if you really want the data in capitalize format, add following class first:
And then,
Now to capitalize every word:
To capitalize only first word:
对于 EditText 中的大写,您可以选择以下两种输入类型:
textCapSentences
这将使每个句子中第一个单词的第一个字母大写。
文字大写
这将使每个单词的第一个字母成为大写。
如果您想要这两个属性,只需使用 |使用这两个属性进行签名
For Capitalisation in EditText you can choose the below two input types:
textCapSentences
This will let the first letter of the first word as Capital in every sentence.
textCapWords
This will let the first letter of every word as Capital.
If you want both the attributes just use | sign with both the attributes
我可以向你保证,两个答案都会使首字母大写,并且不会使 edittext 单行。
如果您想在 XMl 中执行此操作,请使用以下代码
如果您想在活动/片段等中执行此操作,请使用以下代码
PS:如果您有任何其他属性,也可以使用管道“|”轻松添加它符号,只需确保 xml 中属性属性之间没有空格即可
I can assure you both the answers will make first letter capital and will not make edittext single line.
If you want to do it in XMl below is the code
If want to do it in activity/fragment etc below is the code
PS: If you are having nay other property also you can easily add it with a pipe "|" symbol, just make sure there is no space in xml between the attribute properties
在布局 XML 文件中:
设置
android:inputType
在您的
EditText
上将每个句子的第一个单词的第一个字母作为大写或
android:inputType="textCapWords"
在您的 EditText 上将每个单词的第一个字母作为大写In your layout XML file :
set
android:inputType="textCapSentences"
On your
EditText
to have first alphabet of the first word of each sentence as capitalOr
android:inputType="textCapWords"
on your EditText to have first alphabet of each word as capital在 XML 和 JAVA 文件中设置输入类型,如下所示,
在 XML 中,
它还允许多行,并且在 JAVA 文件中,
确保键盘的自动大写设置为启用 。
Set input type in XML as well as in JAVA file like this,
In XML,
It will also allow multiline and in JAVA file,
make sure your keyboard's Auto-Capitalization setting is Enabled.
使用此代码仅将 EditText 的首字母大写
MainActivity.xml
MainActivity.java
use this code to only First letter capitalization for EditText
MainActivity.xml
MainActivity.java
试试这个代码,它会将所有单词的第一个字符大写。
- 为 EditText 视图设置 addTextChangedListener
edt_text.addTextChangedListener(watcher);
- 添加 TextWatcher
Try This Code, it will capitalize first character of all words.
- set addTextChangedListener for EditText view
edt_text.addTextChangedListener(watcher);
- Add TextWatcher
我创立了我的解决方案:
你有两种方法来解决它
在 java :
和 xml 中:
i founded and my solution :
you have 2 way to resolve it
in java :
and xml :
早些时候,它曾经是
android:capitalize="words"
,现已弃用。推荐的替代方案是使用android:inputType="textCapWords"
请注意,这仅在您的设备键盘启用“自动大写设置”时才有效。
要以编程方式执行此操作,请使用以下方法:
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Earlier it used to be
android:capitalize="words"
, which is now deprecated. The recommended alternative is to useandroid:inputType="textCapWords"
Please note that this will only work if your device keyboard Auto Capitalize Setting enabled.
To do this programatically, use the following method:
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
以编程方式
Programmatically
如果您在 styles.xml 中编写样式,则
删除 android:inputType 属性并添加以下行
if you are writing styles in styles.xml then
remove android:inputType property and add below lines