简体   繁体   English

如何在Android Studio中将图像与文本链接?

[英]How to link images with texts in android studio?

I'm trying to make an app that display a text for each image, I have imported four images and four text views , but when i press on any of the image it is display the same text. 我正在尝试制作一个为每个图像显示文本的应用程序,我已经导入了四个图像和四个文本视图,但是当我按任意图像时,它会显示相同的文本。 and display the 4 texts in the same time 并同时显示4个文本

how to display a different text with each image. 如何在每个图像上显示不同的文本。 (i am working in the Main2Activity) (我正在Main2Activity中工作)

xml code 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 CODE JAVA代码

public class Main2Activity extends Activity implements View.OnClickListener{ TextView t1,t2,t3,t4, textview; 公共类Main2Activity扩展Activity实现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;
    }}}

You need to implement View.OnClickListener in your activity where TextViews are declared. 您需要在声明TextViews的活动中实现View.OnClickListener。 Code should look somehow like this 代码应该看起来像这样

@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;
    }}}

If you have more then one TextView then you need to .setOnClickListener(this) on every of them 如果您有多个TextView,则需要在每个上都设置.setOnClickListener(this)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM