如何检查Android中是否选中了复选框?

时间:2015-10-25 03:44:16

标签: java android android-layout android-menu

我正在使用以下代码在checkbox中添加Android Action Bar

 <item
    android:id="@+id/action_checkbox"
    app:actionViewClass="android.widget.CheckBox"
    android:title="@string/action_check"
    app:showAsAction="always"
    />

如何在android中找到上面checkbox的值是否被选为Menu item

3 个答案:

答案 0 :(得分:4)

您可以使用isChecked()获取CheckBox的检查状态:

CheckBox checkBox = (CheckBox) findViewById(R.id.action_checkbox);

if(checkBox.isChecked())
   // do something
else 
   // do something else

答案 1 :(得分:2)

您管理euclid :: Integer -> Integer -> Integer euclid 0 0 = error "GCD(0,0) is undefined" euclid a 0 = a euclid a b = euclid b (a `emod` b) -- Returns the least common multiple of a and b, using the definition of lcm -- given in class (in terms of the gcd). DO NOT use the built-in `lcm` function. lCM :: Integer -> Integer -> Integer lCM 0 0 = error "LCM(0,0) is undefined" lCM a b = a*b `ediv` (euclid a b) -- extGCD a b -- Returns the GCD of a and b, along with x and y such that -- GCD(a,b) = ax + by -- calculated via the recursive extended Euclidean algorithm presented in -- class. extGCD :: Integer -> Integer -> (Integer,Integer,Integer) extGCD 0 0 = error "extGCD(0,0) is undefined" extGCD a 0 = (1,0,a) -- Base case extGCD a b = let (q,r) = a `eDivMod` b -- q and r of a/b (c,x,y) = extGCD b r -- Recursive call in (x,c-q*x, y) -- Recursive results -- coprime a b -- Returns True if a and b are coprime (have no common factors) coprime :: Integer -> Integer -> Bool coprime 0 0 = error "coprime(0,0) is undefined" coprime a b = (euclid a b) == 1 -- minv a n -- Returns the modular inverse of a mod n. Assumes that a and n are coprime. minv :: Integer -> Integer -> Integer minv a n = 点击事件

的Java代码
checkbox

你的菜单看起来不错

 @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.menu_main, menu);
       MenuItem check = menu.findItem(R.id.action_checkbox);
       CheckBox c_box =(CheckBox) check.getActionView();
       c_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b){
                Toast.makeText(getActivity(), "checked",
                        Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(getActivity(), "unchecked",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
      return true;
   }

希望它有效..如果你遇到任何问题,请告诉我。

答案 2 :(得分:1)

请参阅Documentation以获取类似的简单问题。

onOptionsItemSelected也是Menu的一部分。