我正在设置Click Counter 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" />
答案 0 :(得分:1)
您可以将代码放在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();
}
});