我面临TextView
在 android 中设置文本的问题,我的代码是:
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`
}
}
请给我继续开发的方法。