首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Android初学者(添加按钮)

Android初学者(添加按钮)
EN

Stack Overflow用户
提问于 2012-01-12 18:16:46
回答 1查看 219关注 0票数 0

我是安卓系统的初学者,正在制作我的第一个应用程序,即我使用了3个textViews和1个名为Add的按钮。当我点击这个按钮时,应该会添加2个textViews的值,并显示在third textView中。我使用了eventListener和buttonClick event,但它不起作用。请指导我添加按钮的代码。

EN

回答 1

Stack Overflow用户

发布于 2012-01-12 20:18:53

好的,我来帮你。首先创建xml,如下所示:

代码语言:javascript
代码运行次数:0
运行
复制
    <?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代码来完成您想要的操作:

代码语言:javascript
代码运行次数:0
运行
复制
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);
        }
    });
}

}

希望能对你有所帮助。好好享受吧。:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8833366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档