簡體   English   中英

如何在Android Studio中將圖像與文本鏈接?

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

我正在嘗試制作一個為每個圖像顯示文本的應用程序,我已經導入了四個圖像和四個文本視圖,但是當我按任意圖像時,它會顯示相同的文本。 並同時顯示4個文本

如何在每個圖像上顯示不同的文本。 (我正在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擴展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;
    }}}

您需要在聲明TextViews的活動中實現View.OnClickListener。 代碼應該看起來像這樣

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

如果您有多個TextView,則需要在每個上都設置.setOnClickListener(this)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM