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.
当前回答
试试这个代码,它将大写所有单词的第一个字符。
-为EditText视图设置addTextChangedListener
edt_text.addTextChangedListener(观察者);
-添加文本观察者
TextWatcher watcher = new TextWatcher() {
int mStart = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStart = start + count;
}
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
String capitalizedText;
if (input.length() < 1)
capitalizedText = input;
else if (input.length() > 1 && input.contains(" ")) {
String fstr = input.substring(0, input.lastIndexOf(" ") + 1);
if (fstr.length() == input.length()) {
capitalizedText = fstr;
} else {
String sstr = input.substring(input.lastIndexOf(" ") + 1);
sstr = sstr.substring(0, 1).toUpperCase() + sstr.substring(1);
capitalizedText = fstr + sstr;
}
} else
capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);
if (!capitalizedText.equals(edt_text.getText().toString())) {
edt_text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
edt_text.setSelection(mStart);
edt_text.removeTextChangedListener(this);
}
});
edt_text.setText(capitalizedText);
}
}
};
其他回答
我也遇到了同样的问题,只是分享了我的发现。可能会帮助你和其他人…
在你的布局上试试这个。在EditText中添加下面的行。
android:inputType="textCapWords|textCapSentences"
对我来说很好..希望它也适用于你…
在你的布局XML文件中:
将android: inputType = " textCapSentences " 在您的EditText中,将每个句子的第一个单词的首字母作为大写 或者android:inputType="textCapWords"在你的EditText有第一个字母的每个单词作为资本
以前它是android:capitalize="words",现在已弃用。建议使用android:inputType="textCapWords"
请注意,这将只在您的设备键盘自动大写设置启用。
要通过编程实现,请使用以下方法:
setInputType (InputType。TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
使用此代码仅第一个字母大写的EditText
MainActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="true">
</EditText>
</RelativeLayout>
MainActivity.java
EditText et = findViewById(R.id.et);
et.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
{
if (et.getText().toString().length() == 1 && et.getTag().toString().equals("true"))
{
et.setTag("false");
et.setText(et.getText().toString().toUpperCase());
et.setSelection(et.getText().toString().length());
}
if(et.getText().toString().length() == 0)
{
et.setTag("true");
}
}
public void afterTextChanged(Editable editable) {
}
});
只需在EditText元素中使用android:inputType="textCapWords"即可。
例如:
<EditText
android:id="@+id/txtName"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.7"
android:inputType="textCapWords"
android:textColorHint="#aaa"
android:hint="Name Surname"
android:textSize="12sp" />
请参考以下链接: http://developer.android.com/reference/android/widget/TextView.html#attr_android%3ainputType
推荐文章
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 如何检查JSON键是否存在?
- ImageView -有高度匹配宽度?
- 如何确定在android文件的MIME类型?
- 这是在Android中获取用户位置的好方法
- Android从左到右幻灯片动画
- 如何检索视图的维度?
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态