我是安卓系统的初学者,正在制作我的第一个应用程序,即我使用了3个textViews和1个名为Add的按钮。当我点击这个按钮时,应该会添加2个textViews的值,并显示在third textView中。我使用了eventListener和buttonClick event,但它不起作用。请指导我添加按钮的代码。
发布于 2012-01-12 12:18:53
好的,我来帮你。首先创建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>
<TextView android:text="TextView" android:id="@+id/textView2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
然后使用下面的Java代码来完成您想要的操作:
public class HelperStackOverflowActivity 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 = (TextView)findViewById(R.id.textView1);
tv2 = (TextView)findViewById(R.id.textView2);
tv3 = (TextView)findViewById(R.id.textView3);
tv1.setText("Hello");
tv2.setText("World");
Button add = (Button)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);
}
});
}
}
希望能对你有所帮助。好好享受吧。:)
https://stackoverflow.com/questions/8833366
复制相似问题