简体   繁体   English

如何去除 TextView 末尾多余的空格?

[英]How to remove extra spaces at the end of the TextView?

I'm trying to remove the extra spaces at the end of the text in "TextView", but to no avail.我正在尝试删除“TextView”中文本末尾的多余空格,但无济于事。 Here is my code.这是我的代码。

private void userInfo() {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(profileid);
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (getContext() == null){
                return;
            }

            User user = snapshot.getValue(User.class);
            
            username.setText(user.getUsername()); ...

THIS IS MINE "MYTEXTㅤㅤ"ㅤ I WANT TO GET THIS: "MYTEXT"这是我的“MYTEXTㅤㅤ”ㅤ我想得到这个:“MYTEXT”

If user.getUsername() returns a String, try this:如果user.getUsername()返回一个字符串,试试这个:

user.getUsername().trim()

Use trim() on textview Text.在 textview 文本上使用 trim()。 It will trim spaces from beginning and end of the String.它将从字符串的开头和结尾修剪空格。

Use this:用这个:

String textviewText = ((String) tv.getText()).trim();        

You can use trim() function to remove extra space in end of your text您可以使用 trim() function 删除文本末尾的多余空格

user.getUsername().trim();

Actually you should trim() the text when you collect the user input (not just name):实际上,当您收集用户输入(不仅仅是名称)时,您应该trim()文本:

editText.getText().toString().trim()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM