![](/img/trans.png)
[英]Is there a way to tie multiple if statements to one else statement?
[英]How to tie If Statements together into one bigger If Statement in Java
我有一个基本程序。 我希望根据用户的点击次数设置各个按钮的背景,如果所有按钮的背景相同,则用户将继续进行下一个活动。 足够简单,或者我想...我似乎无法弄清楚如何将IF语句相互比较。 我花了一个小时编写和重写代码,试图找到答案。 我必须要有一个简单的答案,就是无法解决问题。
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
Button a1;
Button a2;
Button a3;
Button a4;
static int count1=0;
static int count2=0;
static int count3=0;
static int count4=0;
public void addListenerOnButton() {
final Button a1 = (Button) findViewById(R.id.a1);
a1.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v) {
count1=count1+1;
if(count1==1||count1==4||count1==8)
{ a1.setBackgroundColor(Color.RED);
Toast.makeText(getApplicationContext(),"Button clicked !", Toast.LENGTH_LONG).show();}
else if(count1==2||count1==6)
{a1.setBackgroundColor(Color.BLUE);
Toast.makeText(getApplicationContext(),"Button clicked" +count1, Toast.LENGTH_LONG).show();}
else{a1.setBackgroundColor(Color.YELLOW);
Toast.makeText(getApplicationContext(),"Button clicked" +count1, Toast.LENGTH_LONG).show();
}}});
final Button a2 = (Button) findViewById(R.id.a2);
a2.setOnClickListener(new OnClickListener() {
int clickcount=0;
@Override
public void onClick(View v) {
clickcount=clickcount+1;
if(clickcount==2||clickcount==4||clickcount==7)
{ a2.setBackgroundColor(Color.RED);
Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
else if(clickcount==1||clickcount==3)
{a2.setBackgroundColor(Color.BLUE);
Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
else{a2.setBackgroundColor(Color.YELLOW);
Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
}}});
final Button a3 = (Button) findViewById(R.id.a3);
a3.setOnClickListener(new OnClickListener() {
int clickcount=0;
@Override
public void onClick(View v) {
clickcount=clickcount+1;
if(clickcount==1||clickcount==2||clickcount==6)
{ a3.setBackgroundColor(Color.RED);
Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
else if(clickcount==4||clickcount==5||clickcount==7)
{a3.setBackgroundColor(Color.BLUE);
Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
else{a3.setBackgroundColor(Color.YELLOW);
Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
}}});
final Button a4 = (Button) findViewById(R.id.a4);
a4.setOnClickListener(new OnClickListener() {
int clickcount=0;
@Override
public void onClick(View v) {
clickcount=clickcount+1;
if(clickcount==3||clickcount==5||clickcount==7||clickcount==8)
{ a4.setBackgroundColor(Color.RED);
Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();}
else if(clickcount==2||clickcount==9)
{a4.setBackgroundColor(Color.BLUE);
Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();}
else{a4.setBackgroundColor(Color.YELLOW);
Toast.makeText(getApplicationContext(),"Button clicked" +clickcount, Toast.LENGTH_LONG).show();
}}});
}
}
public void checkFunc(){
if(count1==1)
{
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/"));
startActivity(browserIntent);
}
}
}
我考虑实现目标的一种方法是遵循以下原则
if((a1.setBackgroundColor(Color.BLUE)==true)|| (a2.setBackgroundColor(Color.BLUE)==true))
编辑**因此,尝试了几件事之后,我对代码进行了一些编辑,但仍然无法正常工作。 还有其他建议吗?
您可以使用静态变量进行比较。 还要全局声明变量。 这不是一个好方法,但是可以这样:
//global static vars
static int count1=0;
static int count2=0;
static int count3=0;
static int count4=0;
//make sure you include your conditions. I've added this just for reference
public void addListenerOnButton() {
a1.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v)
{
count1++;
}
}
a2.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v)
{
count2++;
}
}
//And so on...
}
public void checkFunc()
{
if(count1==count2==count3==count4)
{
//do something
}
}
我建议在类中声明clickCount变量,而不是在click handler中声明。 因此,您可以通过每个clickHandler方法获得每个按钮的点击计数。 因此,编写一个比较clickCount的方法,然后在每个clickHandler中调用此方法。
private int clickCount1, clickCount2, clickCount3, clickCount4;
private void compareClickCounts(){
// Compare clickCounts and change buttons properties
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.