【问题标题】:Get text length in characters number Android Java以字符数Android Java获取文本长度
【发布时间】:2022-01-24 15:32:06
【问题描述】:

我有一个文本框 (EditText),我将在其中插入文本,以及一个计算该文本长度并将其显示在另一个文本框中的按钮。我应该如何编写那个按钮?

【问题讨论】:

    标签: java android android-studio android-layout


    【解决方案1】:
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1=findViewById(R.id.b1);
        Button b2=findViewById(R.id.b2);
        EditText t1=findViewById(R.id.t1);
        TextView t2;
        t2 = findViewById(R.id.t2);
    
    
        b1.setOnClickListener(view -> {
            int nb=0;
            for(int i=0; i<t1.length(); ++i){
                nb++;
            }
            t2.setText(Integer.toString(nb));
        });
    
        b2.setOnClickListener(view -> {
            t2.setText(t1.getText().toString());
        });
    }
    

    【讨论】:

    • 你可以简单地使用 t2.setText(Integer.toString(t1.getText().length()));
    • 您已经有了答案,为什么还要提出这个问题? @coderGirl
    • 嘿@MohakShah 我在提出问题后找到了解决方案,所以我教我保留问题并将结果分享给其他人,谢谢您的输入。
    猜你喜欢
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2011-03-23
    • 2021-01-28
    • 2011-04-12
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多