本文介绍了如何设置TextView的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对着在Android上的文本设置为的TextView 的问题,我的code是:

I am facing a problem of setting the text to TextView in android my code is :

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        final TextView text = (TextView) findViewById(R.id.textView1);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final String string = input.getText();
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                text.setText(string);
            }
        });
    }
}

如果我写

    final Editable string = input.getText();

那么它的工作原理...... !!!!

then it works.....!!!!

现在我想的的EditText 数据发送到下一个活动是这样的:

Now I want to send data of EditText to next Activity like this :

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        final TextView text = (TextView) findViewById(R.id.textView1);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final Editable string = input.getText();
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Second.class);
                intent.putExtra("thetext", string);
                startActivity(intent);
            }
        });
    }
}

Second.java 类,我得到 StringExtra 以这种方式:

and in Second.java class I get StringExtra in this way:

public class Second extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        TextView text = (TextView) findViewById(R.id.textView2);
        String string = getIntent().getExtras().getString("thetext", "not found");
        text.setText(string); /// Here the text is not shown but the default message "not found" is set to `TextView`
    }
}

请给我的方式继续发展。

Please give me way to proceed in development.

推荐答案

我认为这个问题是你实际上是把一个编辑的意图,而不是一个字符串。虽然接近,它们不是一回事。如果你的ToString(),您可编辑得到一个String对象,并放进了意图,你应该能够把它找回来用的getString就像你做的事情。

I think the problem is you're actually putting an "Editable" in the intent, not a String. Although close, they're not the same thing. If you toString() your Editable to get a String object and put that in the intent, you should be able to get it back out with getString like you're doing.

这篇关于如何设置TextView的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:02