如何设置TextView的文本?

时间:2012-05-01 14:34:05

标签: android textview

我遇到了将文本设置为TextView的问题,我的代码是:

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();

然后它有效..... !!!!

现在我想将EditText的数据发送到下一个Activity,如下所示:

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

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`
    }
}

请指导我继续开发。

6 个答案:

答案 0 :(得分:5)

问题应该是,您发送的是Editable,而不是String。试试这个:

final String string = input.getText().toString();

答案 1 :(得分:3)

我认为问题是你实际上在意图中放置了“可编辑”,而不是字符串。虽然很接近,但它们并不是一回事。如果你toString()你的Editable得到一个String对象并把它放在intent中,你应该能够像你一样用getString将它取回。

答案 2 :(得分:2)

     private TextView mTextView;
     private String mString;

mTextView = (TextView) findViewById(R.id.tv);
mString = "hello everyone ! how r u?";
mTextView.setText(mString);

答案 3 :(得分:1)

尝试这样的事情:

public class Main extends Activity {
    EditText input;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView text = (TextView) findViewById(R.id.textView1);
        input = (EditText) findViewById(R.id.editText1);

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Second.class);
                intent.putExtra("thetext", input.getText().toString());
                startActivity(intent);
            }
        });
    }
}

(提示:发布代码的最简单方法是粘贴代码,选择代码,然后使用crtl + k缩进/格式化代码。)

答案 4 :(得分:0)

根据android文档,放入额外内容的字符串名称必须包含一个包前缀...即som.arshay.dev.thetext 其次,getExtras()返回一个bundle,这不是你添加的。你需要 getStringExtra( name )

答案 5 :(得分:0)

在此行中使用

final String string = input.getText().toString(); 

而不是

final String string = input.getText();