2012-01-12 54 views
0

我是Android的初学者,正在制作我的第一个应用程序,即我已经拍摄了3个textViews和1个名为Add的按钮。当我点击这个按钮时,应该添加2个textViews的值并显示为第三个textView。我曾使用eventListener和buttonClick事件,但它不起作用。请引导我与添加按钮的代码。Android初学者(添加按钮)

+0

哟有多少尝试?请输入一些代码,我们会帮助你。 – 2012-01-12 12:06:08

回答

1

好吧,我帮你。 首先创建如下的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); 
     } 
    }); 
} 

}

希望它会帮助你。 享受。 :)