我有一個基本程序。我希望根據用戶點擊次數設置各個按鈕的背景,然後如果所有按鈕具有相同的背景,用戶將繼續下一個活動。很簡單,或者我想......我似乎無法弄清楚如何比較IF語句和其他語句。我花了一個小時寫作和重寫代碼,試圖找到答案。必須有一個簡單的答案,我錯過了,只是無法包圍我的思緒。我曾考慮實現我的目標如何將If語句組合成一個更大的If語句在Java中
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))
編輯線的東西**所以在嘗試了幾件事情後,我編輯我的代碼位和斜面仍然得到它的工作。還有什麼建議?
「如何將IF語句與彼此進行比較」。你什麼意思? –
目前還不清楚你打算如何處理這個問題,但是一開始,也許有不同的方法可以幫助你明確你要去的地方。 – Makoto
我想要一個if語句,說「如果按鈕A1的背景是X,按鈕A2的背景是X然後去行動」 – Oliver