gpt4 book ai didi

java - 更改 textview 时实现图像更改

转载 作者:行者123 更新时间:2023-11-30 03:52:13 25 4
gpt4 key购买 nike

我正在设置点击计数器 android 应用程序。最大计数为 500,计数显示在 TextView 字段中。
我希望能够在计数介于 0-100 之间时显示一幅图像,然后在计数介于 101-400 之间时显示另一幅图像,然后在 401-500 之间再次显示第一幅图像。我是 Java 的新手,不知道如何设置它。到目前为止,这是我的代码:

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.preference.PreferenceManager;

public class wazeefa extends Activity {

//Count Button
TextView txtCount;
Button btnCount;
int count = 0;
Button wmute;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wazeefa);

//button sound
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.countbutton);

txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.wclick);

btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
count++;
if (count > 500)
count = 0;
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
}});
;}
}

根据 David/Shreya 的建议,新代码如下所示:

public void onClick(View V) {
ImageView image = (ImageView) findViewById(R.id.imageview);
count++;
if (count > 500) count = 0;
if (count < 1) image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 100) image.setImageResource(R.drawable.zikrimage);
if (count > 400) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
}});
;}

但是当达到相关计数时图像不会改变...

ImageView 的 XML 代码:

<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

最佳答案

您可以将更改图像的代码放在 btnCount 的 onClickListener 中。

btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
ImageView image = findViewById(...); // ... is the id of ImageView
count++;
if (count > 500) count = 0;
if (count > 100) image.setDrawable(...); // 100~200
if (count > 200) image.setDrawable(...); // 200~300
// ... and then 300~400 and 400~500
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
}
});

关于java - 更改 textview 时实现图像更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13963151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com