1

我试过这段代码来显示一个插入的文本视图。那么,如果我想显示两个呢?

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);         
    setContentView(R.layout.activity_main);
    hasil = (TextView)findViewById(R.id.textView1);     
}       

public void kirim(View v) {
    EditText chat = (EditText)findViewById(R.id.editText1);
    hasil.setText(chat.getText().toString());   
}
4

5 回答 5

2

您不能启动组件, (EditText)findViewById(R.id.editText1 + R.id.editText2);请先参考Android Development Doc。

您可以使用以下代码:

TextView hasil;
EditText chat1,chat2;
protected void onCreate(Bundle savedInstanceState) { 

 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main);

 hasil = (TextView)findViewById(R.id.textView1);
 chat1 = (EditText)findViewById(R.id.editText1);
 chat2 = (EditText)findViewById(R.id.editText2);
    }

    public void kirim(View v) {

      hasil.setText(chat1.getText().toString()+" "+chat2.getText().toString());

    }
于 2013-11-08T06:35:10.883 回答
0
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);      
   setContentView(R.layout.activity_main);
  hasil = (TextView)findViewById(R.id.textView1);   

  }         

public void kirim(View v)   {       
EditText chat = (EditText)findViewById(R.id.editText1);
EditText chat2 = (EditText)findViewById(R.id.editText2);
 hasil.setText(chat.getText().toString()+""+chat2.getText().toString());  
}
于 2013-11-08T06:38:53.020 回答
0

其初始化视图的方法无效!

EditText chat = (EditText)findViewById(R.id.editText1 + R.id.editText2);

你所要做的@

public void kirim(View v)   {       
editboxchat1 = (EditText)findViewById(R.id.editText1);
 editboxchat2 = (EditText)findViewById(R.id.editText2);
hasil.setText(editboxchat1.getText().toString()+""+editboxchat2.getText().toString()); 

}
于 2013-11-08T06:41:49.483 回答
0

findViewById (int id)

从在 onCreate(Bundle) 中处理的 XML 中查找由 id 属性标识的视图。它只是查看视图层次结构并返回对具有请求 viewId 的视图的引用。

EditText chat = (EditText)findViewById(R.id.editText1 + R.id.editText2);

We can not add two views like above. findViewById will just try to find the view.
于 2013-11-08T06:43:11.820 回答
0
TextView hasil;
EditText edit1,edit2;
protected void onCreate(Bundle savedInstanceState) { 

 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main);

 hasil = (TextView)findViewById(R.id.textView1);
 edit1 = (EditText)findViewById(R.id.editText1);
 edit2 = (EditText)findViewById(R.id.editText2);
 String _s = edit1.getText().toString();
 String _ss = edit2.getText().toString();
 String _sss = _s + _ss;
}

public void kirim(View v) {

  hasil.setText(_sss);

}
于 2013-11-08T06:58:51.187 回答