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);
}
}
};
其他回答
在XML编辑文本中应用以下行。
android:inputType="textCapSentences|textMultiLine"
它还允许多线支持。
以编程方式
TextView firstline = (TextView) findViewById(R.id.firstline);
firstline.setAllCaps(true);
试试这个代码,它将大写所有单词的第一个字符。
-为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);
}
}
};
以前它是android:capitalize="words",现在已弃用。建议使用android:inputType="textCapWords"
请注意,这将只在您的设备键盘自动大写设置启用。
要通过编程实现,请使用以下方法:
setInputType (InputType。TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
在你的布局XML文件中:
将android: inputType = " textCapSentences " 在您的EditText中,将每个句子的第一个单词的首字母作为大写 或者android:inputType="textCapWords"在你的EditText有第一个字母的每个单词作为资本
推荐文章
- 何时在Android中使用RxJava,何时使用Android架构组件中的LiveData ?
- 如何在Android项目中使用ThreeTenABP
- 指定的子节点已经有一个父节点。你必须先在子对象的父对象上调用removeView() (Android)
- 我的Android设备没有出现在adb设备列表中
- 在没有安装apk的情况下获取Android .apk文件的VersionName或VersionCode
- Fragment onResume() & onPause()不会在backstack上被调用
- 如何设置基线对齐为假提高性能在线性布局?
- 如何获得当前屏幕方向?
- 如何在Android中渲染PDF文件
- 我如何解决错误“minCompileSdk(31)指定在一个依赖的AAR元数据”在本机Java或Kotlin?
- 如何改变TextInputLayout的浮动标签颜色
- Android工作室如何运行gradle同步手动?
- 如何以编程方式在我的EditText上设置焦点(并显示键盘)
- 如果在片段和活动中同时定义,则不会在片段中调用onRequestPermissionsResult
- 方法setDrawerListener已弃用