如何在android中单击按钮时向textview显示两个插入的文本

时间:2013-11-08 06:32:27

标签: android button textview

我已尝试使用此代码显示一个插入的textview。那么,如果我想显示两个呢?

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

5 个答案:

答案 0 :(得分:2)

您无法启动(EditText)findViewById(R.id.editText1 + R.id.editText2);等组件,请先参阅Android Development文档

您可以使用以下代码:

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

    }

答案 1 :(得分: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());  
}

答案 2 :(得分: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()); 

}

答案 3 :(得分: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.

答案 4 :(得分: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);

}