如何在EditText中自动设置度数符号?

huangapple go评论76阅读模式
英文:

¿How to set automatically degree symbol in EditText?

问题

我正在Android Studio中制作一个应用程序,其中我需要捕获的数据之一是个人的体温。

如图所示,我唯一做的是在EditText中默认放置了度数符号。但是我必须手动将光标移动到符号前面的数字位置。

有人知道如何在EditText中键入温度数据后,使安卓自动将度数符号放置在之后吗?

如何在EditText中自动设置度数符号?

英文:

I am making an application in android studio where one of the data that I need to capture is the temperature of the person.

As shown in the image, the only thing that I did was place the degree symbol as default in the EditText. But I have to manually move the cursor to place the number before the symbol.

Does anyone know how I can make android place the degrees symbol automatically AFTER typing the temperature data in EditText?

如何在EditText中自动设置度数符号?

答案1

得分: 1

你应该在具有回调函数**afterTextChanged(Editable s)的编辑文本上使用TextWatcher,在此函数中,您应该通过连接方式s.toString() + your_char**将所需的字符串/字符放在末尾。

yourEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {
        // 在输入温度后在此处设置数据
        yourEditText.append(" °");
    }
});

参考链接

英文:

You should be using TextWatcher for edit Text which has a callback function afterTextChanged(Editable s) in which you should put your desired String/char at the end by contacting like s.toString() + your_char

yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    // TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    // TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable s) {

    //here you are setting the data after the entry of temprature
    yourEditText.append(" \u00B0");

}
});

Reference

huangapple
  • 本文由 发表于 2020年9月20日 00:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63971074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定