如何将 int 或 char 显示到 TextView 中

发布于 2025-01-07 00:01:02 字数 1016 浏览 3 评论 0原文

我想处理评论输入并写回到 TextView 中。 我做了一个名为 PandaAssistances 的类,

package panda.com.db;

public class PandaAssistents{

    private static int CallOfIndex;
    private static String comment;
    public PandaAssistents(String input){
        this.comment = input;
        this.CallOfIndex=input.indexOf("熊貓");
    }

    public char getCall(){
        return comment.charAt(CallOfIndex);
    }
}

但是当我输入 EditText 并单击按钮时,它会生成错误。

private void btnAction(Button btn){
        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                EditText edt = (EditText)IndexActivity.this.findViewById(R.id.EditText01);
                TextView txt = (TextView)IndexActivity.this.findViewById(R.id.TextView01);
                pa = new PandaAssistents(edt.getText().toString());
                txt.setText(pa.getCall());
            }
        });
    }

我不知道问题是什么

I want to process the comment input and write back into a TextView.
I did a Class named PandaAssistents

package panda.com.db;

public class PandaAssistents{

    private static int CallOfIndex;
    private static String comment;
    public PandaAssistents(String input){
        this.comment = input;
        this.CallOfIndex=input.indexOf("熊貓");
    }

    public char getCall(){
        return comment.charAt(CallOfIndex);
    }
}

But when I field in a EditText and click a button, it generate an error.

private void btnAction(Button btn){
        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                EditText edt = (EditText)IndexActivity.this.findViewById(R.id.EditText01);
                TextView txt = (TextView)IndexActivity.this.findViewById(R.id.TextView01);
                pa = new PandaAssistents(edt.getText().toString());
                txt.setText(pa.getCall());
            }
        });
    }

I don't know what the problem is

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2025-01-14 00:01:02

TextView或EditText的setText需要charsequence或资源id作为输入参数,如果要设置char或int,请在设置到textview之前将其转换为字符串。所以你可以尝试以下操作:

txt.setText(""+pa.getCall());

TextView or EditText's setText requires charsequence or resource id as input parameter, if you want to set char or int, convert to it string before setting into textview. so you can try following:

txt.setText(""+pa.getCall());
嗼ふ静 2025-01-14 00:01:02

您确定 this.CallOfIndex=input.indexOf("熊猫") 实际上返回一个有效的索引而不是 -1 吗?由于您没有对该值进行错误检查,因此如果结果为 -1,则当您调用 comment.charAt(CallOfIndex)CallOfIndex 为 -1 时,您将得到一个 < a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#charAt%28int%29" rel="nofollow">IndexOutOfBoundsException

Are you sure this.CallOfIndex=input.indexOf("熊貓") actually returns a valid index and not -1? Since you are not error checking this value, if the result is -1, when you call comment.charAt(CallOfIndex) with CallOfIndex being -1, you will get an IndexOutOfBoundsException

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文