2016-04-18 6 views
0

私は各画像のテキストを表示するアプリケーションを作成しようとしていますが、4つの画像と4つのテキストビューをインポートしましたが、いずれかの画像を押すと同じテキストが表示されます。同じ時間に4つのテキストを表示するアンドロイドスタジオで画像をテキストとリンクさせるにはどうすればいいですか?

各画像で異なるテキストを表示する方法(iはMain2Activityで働いています)

XMLコード

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="WELCOME " 
    android:id="@+id/t1" 
    android:textSize="50dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="49dp" 
    android:checked="true"/> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="ANDROID" 
    android:id="@+id/t3" 
    android:textSize="50dp" 
    android:layout_above="@+id/t4" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="45dp" 
    android:checked="false"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="STUDIO" 
    android:id="@+id/t4" 
    android:textSize="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="91dp" 
    android:checked="false"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="TO" 
    android:id="@+id/t2" 
    android:layout_below="@+id/t1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="47dp" 
    android:textSize="50dp" 
    android:checked="false"/> 

Javaコード

パブリッククラスMain2ActivityアクティビティがView.OnClickListener { のTextViewのT1、T2、T3、T4、のTextViewを実装延び。

@Override 
public void onCreate (Bundle savedInstanceState, PersistableBundle persistentState) { 
    super.onCreate (savedInstanceState, persistentState); 
    setContentView(R.layout.activity_main); 
    t1 = (TextView) findViewById (R.id.text); 
    t1.setOnClickListener (this); //here you're setting onClickListener to listen for taps 
    textview = (TextView) findViewById (R.id.t1); 
    t2.setOnClickListener (this); 
    textview = (TextView) findViewById (R.id.t2); 
    t3.setOnClickListener (this); 
    textview = (TextView) findViewById (R.id.t3); 
    t4.setOnClickListener (this); 
    textview = (TextView) findViewById (R.id.t4); 
} 

@Override 
public void onClick (View v) { //this is an implementation of OnClickListener interface 
    switch (v.getId()){ //here you're reading id of the taped button and doing something depending on what TextView taped 
     case R.id.t1: 
      t1.setText ("leo"); 
      break; 
     case R.id.t2: 
      t2.setText ("mike"); 
      break; 
     case R.id.t3: 
      t3.setText ("raph"); 
      break; 
     case R.id.t4: 
      t4.setText ("don"); 
      break; 
    }}} 
+0

に.setOnClickListener(これ)に必要なコードは、Androidを使用してみてください何とかなるはずです使用したいright –

答えて

0

TextViewsが宣言されているアクティビティに、View.OnClickListenerを実装する必要があります。 drawableLeft =「画像パス」と同じ場合:あなたは1つのTextView、より多くを持っている場合は、この

@Override 
public void onCreate (Bundle savedInstanceState, PersistableBundle persistentState) { 
    super.onCreate (savedInstanceState, persistentState); 
    setContentView(R.layout.activity_main); 
    t1 = (TextView) findViewById (R.id.t1); 
    t1.setOnClickListener (this); 

    t2 = (TextView) findViewById (R.id.t2); 
    t2.setOnClickListener (this); 

    t3 = (TextView) findViewById (R.id.t3); 
    t3.setOnClickListener (this); 

    t4 = (TextView) findViewById (R.id.t4); 
    t1.setOnClickListener (this); 
} 

@Override 
public void onClick (View v) { //this is an implementation of OnClickListener interface 
    switch (v.getId()){ //here you're reading id of the taped button and doing something depending on what TextView taped 
     case R.id.t1: 
      t1.setText ("leo"); 
      break; 
     case R.id.t2: 
      t2.setText ("mike"); 
      break; 
     case R.id.t3: 
      t3.setText ("raph"); 
      break; 
     case R.id.t4: 
      t4.setText ("don"); 
      break; 
    }}} 

は、あなたがそれらのすべての

+0

私はimage.didを押すとテキストが表示されないので、私は何が変わるかチェックすることができますかwrightを変更するか、修正する必要があります,,,,,, android studioの新機能 – samawi

+0

私は上記のコードを更新しました。 – samawi

+0

あなたのコードに完全に合うように答えを編集して使用しました。 – Arthur

関連する問題