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
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) {
}
});
要大写,您可以对编辑文本执行以下操作:
大写:使每个单词的第一个字母大写:
android:inputType="textCapWords"
使每个句子的第一个字母大写:
android:inputType="textCapSentences"
使每个字母大写:
android:inputType="textCapCharacters"
但这只会改变键盘和用户可以改变模式写小写字母。
因此,如果你真的想要大写格式的数据,这种方法不太受欢迎,首先添加以下类:
public class CapitalizeFirstLetter {
public static String capitaliseName(String name) {
String collect[] = name.split(" ");
String returnName = "";
for (int i = 0; i < collect.length; i++) {
collect[i] = collect[i].trim().toLowerCase();
if (collect[i].isEmpty() == false) {
returnName = returnName + collect[i].substring(0, 1).toUpperCase() + collect[i].substring(1) + " ";
}
}
return returnName.trim();
}
public static String capitaliseOnlyFirstLetter(String data)
{
return data.substring(0,1).toUpperCase()+data.substring(1);
}
}
然后,
现在把每个单词都大写:
CapitalizeFirstLetter.capitaliseName(name);
只大写第一个词:
CapitalizeFirstLetter.capitaliseOnlyFirstLetter(data);
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
或者android:inputType=" textcapsentence "将只工作,如果你的设备键盘自动大写设置启用。
我可以向你保证,两个答案的第一个字母大写,不会使编辑文本单行。
如果你想用XMl来做,下面是代码
android:inputType="textCapWords|textCapSentences"
如果想做它在活动/片段等下面是代码
momentTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_MULTI_LINE)
PS:如果你有其他属性,你也可以很容易地添加一个管道“|”符号,只是确保在xml属性属性之间没有空格
如果您在styles.xml中编写样式,那么
删除android:inputType属性并添加以下行
<item name="android:capitalize">words</item>
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件