2016-09-08 16 views
-2

現在、私はTextViewのリストを持っていますが、初めてクリックするとその背景が変更されます。しかし、私はまた、ユーザーが2度目にその背景を再びクリックしなければならない場合、あるいはその逆の場合も欲しい。次のクリックでボタンの背景を変更する方法

私の既存のコードでは、最初のクリックの背景を変更することができます。それを再びクリックすると、背景が変更されません。ここで

は私のコードです:

public void onClick(View v) { 


     switch (v.getId()) { 
      case R.id.goalText1: 
       if (count <= 2) { 
        goals.add(mGoal1.getText().toString()); 
        mGoal1.setTextColor(getResources().getColor(R.color.black)); 
        mGoal1.setBackgroundColor(getResources().getColor(R.color.white)); 
        count++; 

       } else { 
        Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show(); 
       } 
       break; 

      case R.id.goalText2: 

       if (count <= 2) { 
        goals.add(mGoal2.getText().toString()); 
        mGoal2.setTextColor(getResources().getColor(R.color.black)); 
        mGoal2.setBackgroundColor(getResources().getColor(R.color.white)); 
        count++; 
       } else { 
        Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show(); 
       } 
       break; 

      case R.id.goalText3: 

       if (count <= 2) { 
        goals.add(mGoal3.getText().toString()); 
        mGoal3.setTextColor(getResources().getColor(R.color.black)); 
        mGoal3.setBackgroundColor(getResources().getColor(R.color.white)); 
        count++; 
       } else { 
        Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show(); 
       } 
       break; 

      case R.id.goalText4: 

       if (count <= 2) { 
        goals.add(mGoal4.getText().toString()); 
        mGoal4.setTextColor(getResources().getColor(R.color.black)); 
        mGoal4.setBackgroundColor(getResources().getColor(R.color.white)); 
        count++; 
       } else { 
        Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show(); 
       } 
       break; 
      case R.id.goalText5: 

       if (count <= 2) { 
        goals.add(mGoal5.getText().toString()); 
        mGoal5.setTextColor(getResources().getColor(R.color.black)); 
        mGoal5.setBackgroundColor(getResources().getColor(R.color.white)); 
        count++; 
       } else { 
        Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show(); 
       } 
       break; 
      case R.id.goalText6: 

       if (count <= 2) { 
        goals.add(mGoal6.getText().toString()); 
        mGoal6.setTextColor(getResources().getColor(R.color.black)); 
        mGoal6.setBackgroundColor(getResources().getColor(R.color.white)); 
        count++; 
       } else { 
        Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show(); 
       } 
       break; 
      case R.id.goalText7: 

       if (count <= 2) { 
        goals.add(mGoal7.getText().toString()); 
        mGoal7.setTextColor(getResources().getColor(R.color.black)); 
        mGoal7.setBackgroundColor(getResources().getColor(R.color.white)); 
        count++; 
       } else { 
        Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show(); 
       } 
       break; 


      case R.id.btnGoal: 
       Intent intent = new Intent(this, fiteness_level_selection.class); 


       try { 
        obj.put("SelectedGoal", goals); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       intent.putExtra("GoalJson", obj.toString()); 
       startActivity(intent); 
       break; 


     } 

だから誰も私にそれを達成するための簡単な方法を提案することができます。

+0

textviewをクリックすると、指定した2色以上を切り替えたいですか? –

+0

だから、あなたはtextviewの代わりに色を変更したいですか? (その間に色?) –

+0

白と透明の間の@Protino –

答えて

0

アクティビティにブール値の配列を設定します。

//suppose you've 3 textviews within listview  
public boolean isTransparent[] = {false,false,false}; 

次に、あなたのonClickメソッドでこれを行います。

public void onOnclick(View v){ 
    switch (v.getId()) { 
     //foreach textview do this 
     case R.id.textView1 : 
      int color; 
      if (isTransparent[0]) 
       color = getResources().getColor(android.R.color.transparent); 
      else 
       color = getResources().getColor(R.color.white); 
      isTransparent[0]=!isTransparent[0]; 
      textview1.setBackgroundColor(color); 
      break; 
     case R.id.textView2: 
      // now check for isTransparent[1] 
      and so on ... 
      ... 

    } 
} 
0

あなたが欲しいと思っているのは、boolという変数があり、Buttonの値を確認する必要があることを示すための背景を変更することです。boolの値をチェックする必要があります。あなたのButtonクリック

使用インサイド

Boolean click_firsttime = false; 

このよう

If(click_firsttime == false){ 
//perform task 

// and set bool value to true 
click_firsttime = true; 
}else{ 

// perform task and set bool value 

click_firsttime = false; 
} 
0

まずブール値を作成します。

private boolean foo = true; 

を次に、このようにコードを変更します。

public void onClick(View v) { 


    switch (v.getId()) { 
     case R.id.goalText1: 
      if (foo) { 
       goals.add(mGoal1.getText().toString()); 
       mGoal1.setTextColor(getResources().getColor(R.color.black)); 
       mGoal1.setBackgroundColor(getResources().getColor(R.color.white)); 
       count++; 
       foo = false; 
      } else { 
       // set background to another color 
       foo = true; 
      } 
      break; 
関連する問題