Java 更改textview时实现图像更改
我正在设置一个点击计数器android应用程序。最大计数为500,计数显示在文本视图字段中。Java 更改textview时实现图像更改,java,android,Java,Android,我正在设置一个点击计数器android应用程序。最大计数为500,计数显示在文本视图字段中。 我希望能够在计数介于0-100之间时显示一幅图像,然后在计数介于101-400之间时显示另一幅图像,然后在401-500之间再次显示第一幅图像。 我是Java新手,不知道如何设置它。以下是我目前的代码: import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences.
我希望能够在计数介于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();
}});
;}
public void onClick(视图V){
ImageView图像=(ImageView)findViewById(R.id.ImageView);
计数++;
如果(计数>500)计数=0;
if(计数<1)image.setImageResource(R.drawable.duroodimage);
如果(计数>0)image.setImageResource(R.drawable.duroodimage);
如果(计数>100)image.setImageResource(R.drawable.zikrimage);
如果(计数>400)image.setImageResource(R.drawable.duroodimage);
setText(String.valueOf(count));
mpbutton单击.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();
}
});
您可以将更改图像的代码放在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();
}
});
您的xml布局中有
ImageView
吗?您的xml布局中有ImageView
吗?您好,David,我尝试添加您的建议,正如您在上面看到的。但是我得到以下错误:-类型ViewOh的方法setDrawable(new View.OnClickListener(){},int)未定义,对不起。您需要将View image=findViewById…
更改为ImageView image=(ImageView)findViewById…
@Mustafa Try image.setImageResource(R.drawable.myimagename)@ShreyaShah和Hi,谢谢你消除了这个错误!但是,当点击量达到相关总数时,“图像更改”不起作用…@Mustafa,你需要在问题中添加代码,而不是在答案中添加代码。你好,David,我尝试添加你的建议,如你所见。但是我得到以下错误:-类型ViewOh的方法setDrawable(new View.OnClickListener(){},int)未定义,对不起。您需要将View image=findViewById…
更改为ImageView image=(ImageView)findViewById…
@Mustafa Try image.setImageResource(R.drawable.myimagename)@ShreyaShah和Hi,谢谢你消除了这个错误!但是,当点击量达到相关总数时,“图像更改”不起作用…@Mustafa,您需要将代码添加到问题中,而不是答案中。