我在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`
}
}
請給我的方式繼續在發展。
感謝佈雷登這是我來到了它的問題... –