2012-05-01 65 views
6

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

請給我的方式繼續在發展。

回答

3

我認爲問題在於你實際上在意圖中放置了「可編輯」而不是字符串。雖然接近,但它們並不是一回事。如果你將String()的Editable變成String對象並把它放在intent中,那麼你應該可以像getString一樣將它取出來。

+0

感謝佈雷登這是我來到了它的問題... –

5

問題應該是,你發送的是Editable,而不是String。試試這個:

final String string = input.getText().toString(); 
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縮進/格式化)

0

根據android文檔,放入額外字符串的名稱必須包含一個包前綴...... ie som.arshay.dev.thetext 其次,getExtras()返回一個包,這不是什麼你添加了。您需要 getStringExtra(name)

0

在這一行使用

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

代替

final String string = input.getText(); 
2
 private TextView mTextView; 
    private String mString; 

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