gpt4 book ai didi

android - 如何编写计数器图像以使用 Count++ 进行更改

转载 作者:行者123 更新时间:2023-11-30 01:27:47 27 4
gpt4 key购买 nike

我是 android 编程的新手,我正在制作一个具有动画计数器的应用程序。每次我按下计数按钮计数都会增加,我想根据计数更改 ImageView 图像源

我在 if 条件下执行此操作,而我想动态地执行此操作,因为我有 9999 个最大计数,所以我不能为此写太多 if 条件

TextView textViewHeader;
static String value;
static int count;
static ImageButton imageButtonCountTasbeeh;
ImageView imageViewLsb,imageViewLsb1, imageViewLsb2, imageViewLsb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasbeeh_counter);
imageButtonCountTasbeeh = (ImageButton)findViewById(R.id.imageButtonCountTasbeeh);
textViewHeader = (TextView)findViewById(R.id.editTextTasbeehName);
imageButtonCountTasbeeh.setImageResource(R.drawable.imagebuttontasbeenpageselector);
imageViewLsb = (ImageView)findViewById(R.id.imageViewLsb);
imageViewLsb1 = (ImageView)findViewById(R.id.imageViewLsb1);
imageViewLsb2 = (ImageView)findViewById(R.id.imageViewLsb2);
imageViewLsb3 = (ImageView)findViewById(R.id.imageViewLsb3);

Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getString("tasbeehName");
}
textViewHeader.setText(value);

imageButtonCountTasbeeh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getBaseContext(), "Hello you Just Clicked me", Toast.LENGTH_LONG).show();
count++;
if(count<10) {
int resID = getResources().getIdentifier("n" + count, "drawable", getPackageName());
imageViewLsb.setImageResource(resID);
}else if(count<20 && count>=10)
{
int count1 = count-10;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());

imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n1);

}else if(count<30 && count>=20)
{
int count1 = count-20;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());

imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n2);
}else if(count<40 && count>=30)
{
int count1 = count-30;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());

imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n3);
}else if(count<50 && count>=40)
{
int count1 = count-40;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());

imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n4);
}else if(count<60 && count>=50)
{
int count1 = count-50;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());

imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n5);
}else if(count<70 && count>=60)
{
int count1 = count-60;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());

imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n6);
}else if(count<80 && count>=70)
{
int count1 = count-70;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());

imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n7);
}else if(count<90 && count>=80)
{
int count1 = count-80;
int resID = getResources().getIdentifier("n" + count1, "drawable", getPackageName());

imageViewLsb.setImageResource(resID);
imageViewLsb1.setImageResource(R.drawable.n8);
}



}
});




}

最佳答案

试试这个:

imageButtonCountTasbeeh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getBaseContext(), "Hello you Just Clicked me", Toast.LENGTH_LONG).show();
count++;
if (count > 999)
imageViewLsb3.setImageResource(getResIdDrawableByNumber(count / 1000));

if (count > 99 ) {
int n100 = (count % 1000) / 100;
imageViewLsb2.setImageResource(getResIdDrawableByNumber(n100));
}

if (count > 9) {
int number10 = (count % 100) / 10;
imageViewLsb1.setImageResource(getResIdDrawableByNumber(number10));
}

imageViewLsb.setImageResource(getResIdDrawableByNumber(count % 10));
}
});

使用该方法通过数值获取对应的图片资源id:

public int getResIdDrawableByNumber(int count){    
return = getResources().getIdentifier("n" + count, "drawable", getPackageName());
}

希望这对您有所帮助!

关于android - 如何编写计数器图像以使用 Count++ 进行更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36210063/

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