0

私はAndroidの初心者で、最初のアプリを作成しています。ここでは、3 つの textView と Add という名前のボタンを取得しました。このボタンをクリックすると、2 つの textView のコンテンツを 3 番目の textView に表示する必要があります。eventListener と buttonClick イベントを使用しましたが、機能しません。追加ボタンのコードを教えてください。

4

1 に答える 1

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 
   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);
        }
    });
}
于 2012-01-12T12:18:53.380 に答える