2013-05-01 30 views
0

正常工作,我已經在Android的做了一個簡單checkbo [X演示程序,因爲我已經把chekbox和button.I希望的是,當按下chekbox並按下按鈕泰德表示乾杯「我接受」,否則當產生按下按鈕舉杯爲‘我不接受’...我造的,但是問題是,當我未選中的chekbox..it直接顯示吐司(無需點擊按鈕);複選框不是在Android的

mainActivity.java

package com.example.chechboxdemo; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
CheckBox c1; 
Button b; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     c1=(CheckBox)findViewById(R.id.checkBox1); 
     b=(Button)findViewById(R.id.button1); 
     c1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
       // TODO Auto-generated method stub 

       if(c1.isChecked()){ 

        b.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View arg0) { 
          // TODO Auto-generated method stub 
          Toast.makeText(getApplicationContext(), "i accept", 1).show(); 
         } 
        }); 
       } 
       else{ 
        Toast.makeText(getApplicationContext(), "i don't accept", 1).show(); 

       } 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 
+0

您已設置按鈕的''onClick'複選框B'裏面'onCheckedChanged',儘量外面放置 – Pragnani 2013-05-01 14:41:15

+0

讓我知道什麼是你想實現 – Pragnani 2013-05-01 14:41:32

回答

1

你不應該使用checkChanged監聽器,除非你想要的東西,當用戶選中/取消選中該複選框的情況發生。因爲只需要當按鈕被點擊,你只需要你的onClickListener的動作發生,它應該是您onCheckChangedListener的

嘗試這樣的:

public class MainActivity extends Activity { 
CheckBox c1; 
Button b; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     c1=(CheckBox)findViewById(R.id.checkBox1); 
     b=(Button)findViewById(R.id.button1); 
     c1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
       // You don't need to use onCheckChangeListener. 
      } 
     }); 

     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       if(c1.isChecked()){ 
        Toast.makeText(getApplicationContext(), "i accept", 1).show(); 
       }else{ 
        Toast.makeText(getApplicationContext(), "i don't accept", 1).show(); 
       } 
      } 
     }); 
    }//end onCreate 
}//end MainActivity 
+0

感謝名單老闆.. 。!有一個nyc日.. :) – jigar 2013-05-01 15:25:23

0

移動c1.isChecked()內按鈕點擊查看如果複選框被選中或不在按鈕上,則根據複選框狀態顯示吐司消息。改變你的代碼:

b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       if(c1.isChecked()){ //<<< check here if checkbox clicked or not 
        Toast.makeText(getApplicationContext(), 
              "i accept", 1).show(); 
       }else{ 
        Toast.makeText(getApplicationContext(), 
             "i don't accept", 1).show(); 
       } 
      } 
     });