Java 如何在android studio中将图像与文本链接?
我正在尝试制作一个为每个图像显示文本的应用程序,我已经导入了四个图像和四个文本视图,但是当我按下任何一个图像时,它都显示相同的文本。并同时显示4个文本 如何在每个图像中显示不同的文本。(我在Main2活动中工作) xml代码Java 如何在android studio中将图像与文本链接?,java,android,android-studio,Java,Android,Android Studio,我正在尝试制作一个为每个图像显示文本的应用程序,我已经导入了四个图像和四个文本视图,但是当我按下任何一个图像时,它都显示相同的文本。并同时显示4个文本 如何在每个图像中显示不同的文本。(我在Main2活动中工作) xml代码 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/t
<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"/>
您需要在声明TextView的活动中实现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;
}}}
如果你有多个文本视图,那么你需要。在每个文本视图上设置一个ClickListener(this)尝试使用android:drawableLeft=“image path”和相同的方法如果你想使用Right,你可以检查我更改了什么,因为我按图像时不显示文本。我是否更改了wright或需要修复。,,,抱歉,我是android研究的新手。我已经编辑了我的答案,以完全符合您的代码,请使用它。我复制了您的代码,但仍然存在相同的问题,没有文本(空白页)。代码中没有错误一切都很好,,,,,同时它也向我表明(没有找到测试)这是原因吗??这是弥赛格。。。。。。。正在运行测试运行已启动完成空测试套件。@samawi,这是因为您正在启动测试,而不是应用程序。只需点击Shift+F10,就可以运行应用程序编译了
@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;
}}}