我是安卓系统的初学者,正在制作我的第一个应用程序。在这里,我获得了3个文本视图和一个名为"添加"的按钮。当我点击这个按钮时,我需要在第三个textView上显示2个textView的内容。我使用了eventListener和buttonClick事件,但它不起作用。请用"添加"按钮的代码指导我。
首先创建如下xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_weight="1"
android:id="@+id/singleEmployee"
android:background="#ffffff">
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
然后使用这个Java代码来完成您想要的操作:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView tv1,tv2,tv3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testing);
tv1 = findViewById(R.id.textView1);
tv2 = findViewById(R.id.textView2);
tv3 = findViewById(R.id.textView3);
tv1.setText("Hello");
tv2.setText("World");
Button add = findViewById(R.id.button1);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv3.setText("");
String tvValue1 = tv1.getText().toString();
String tvValue2 = tv2.getText().toString();
tv3.setText("Value of First Text is: "+tvValue1+".And the value of second TextView is: "+tvValue2);
}
});
}