我以前曾问过这个问题,但收到的帮助很少甚至没有。 我真的不明白什么是错的。 这个代码的重点是,当按下按钮时,检查是否选中了检查表,如果选中,则将1添加到分数,检查分数值并为文本分配输出
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* Created by Ashraf on 3/2/2016.
*/
public class check_Button extends Pop_sallian{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
// Connects The variable to an xml id
TextView output = (TextView) findViewById(R.id.output);
int score = 0;
//sets the variable to 0
OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.concern:
score++;
break;
case R.id.faith:
score++;
break;
case R.id.respect:
score++;
break;
case R.id.education:
score++;
break;
case R.id.community:
score++;
break;
}
}
};
// adds the variables together to form a score
if(score == 0){
output.setText("score of 0");
} else if(score == 1){
output.setText("score of 1");
} else if(score == 2){
output.setText("score of 2");
} else if(score == 3){
output.setText("score of 3");
} else if(score == 4){
output.setText("score of 4");
} else if(score == 5){
output.setText("score of 5");
} else{
output.setText("Unknown");
}
// changes the output text based on score value
}
}
^^清单代码^^
package xyz.ashraf.whoisdelasalle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
/**
* Created by Ashraf on 1/27/2016.
*/
public class Pop_sallian extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.6));
Button checkButton = (Button) findViewById(R.id.check);
checkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Pop_sallian.this, check_Button.class));
// calls the checklist class
}
});
Button okButton = (Button) findViewById(R.id.okButton_sallian);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
^^按钮所在的代码,当按下调用清单代码时^^
谢谢你的帮助
答案 0 :(得分:-1)
您似乎没有为按钮设置事件。
此变量:checkedListener尚未使用。
所以得分值总是等于0。