isChecked()将无法正常工作

时间:2016-03-02 07:03:07

标签: android checkbox textview

我不知道为什么,但isChecked()不起作用,它说无法解析符号。有什么我应该导入的。我还想问一下如何更改Textview谢谢的文字。

package xyz.ashraf.whoisdelasalle;

import android.view.View;
import android.widget.CheckBox;

/**
 * Created by Ashraf on 3/2/2016.
 */
public class check_Button extends Pop_sallian{
CheckBox concern = (CheckBox) findViewById(R.id.concern);
CheckBox faith = (CheckBox) findViewById(R.id.faith);
CheckBox respect = (CheckBox) findViewById(R.id.respect);
CheckBox education = (CheckBox) findViewById(R.id.education);
CheckBox community = (CheckBox) findViewById(R.id.community);

int con = 0;
int fai = 0;
int res = 0;
int edu = 0;
int com = 0;

if (concern.isChecked()) {
    con++;
}
if (faith.isChecked()) {
    fai++;
}
if(respect.isChecked()){
    res++;
}
if(education.isChecked()){
    edu++;
}
if(community.isChecked()){
    com++;
}
}

1 个答案:

答案 0 :(得分:0)

您必须先创建onCreate方法,然后在其中添加代码

 @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.yourActivity);

     CheckBox concern = (CheckBox) findViewById(R.id.concern);
     CheckBox faith = (CheckBox) findViewById(R.id.faith);
     CheckBox respect = (CheckBox) findViewById(R.id.respect);
     CheckBox education = (CheckBox) findViewById(R.id.education);
     CheckBox community = (CheckBox) findViewById(R.id.community);

     int con = 0;
    int fai = 0;
    int res = 0;
    int edu = 0;
    int com = 0;

    if (concern.isChecked()) {
        con++;
    }
    if (faith.isChecked()) {
        fai++;
    }
    if(respect.isChecked()){
        res++;
    }
    if(education.isChecked()){
        edu++;
    }
    if(community.isChecked()){
        com++;
    }


}