[英]Display the number of checked items from another activity
On the second activity i have checkboxes
with radiobuttons
, their state i saving in sharedpreference
and i want to create on first activity something like this: 在第二个活动中,我具有带
checkboxes
radiobuttons
,它们的状态我保存在sharedpreference
并且我想在第一个活动中创建类似以下内容:
" 2 from 10 checkboxes are checked " “选中了10个复选框中的2个”
I do not know how to do it or with what elements of the palette to do it. 我不知道该怎么做或用调色板中的哪些元素来做。
Can i ask for some tricks how to do it best? 我可以问一些技巧如何做到最好吗?
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox cb1,cb2,cb3,cb4;
RadioButton rb1;
cb1 = (CheckBox)findViewById(R.id.cb1);
cb1.setChecked(getFromSP("cb1"));
cb1.setOnCheckedChangeListener(this);
cb2 = (CheckBox)findViewById(R.id.cb2);
cb2.setChecked(getFromSP("cb2"));
cb2.setOnCheckedChangeListener(this);
cb3 = (CheckBox)findViewById(R.id.cb3);
cb3.setChecked(getFromSP("cb3"));
cb3.setOnCheckedChangeListener(this);
cb4 = (CheckBox)findViewById(R.id.cb4);
cb4.setChecked(getFromSP("cb4"));
cb4.setOnCheckedChangeListener(this);
rb1 = (RadioButton)findViewById(R.id.rb1);
rb1.setChecked(getFromSP("rb1"));
rb1.setOnCheckedChangeListener(this);
}
private boolean getFromSP(String key){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.cb1:
saveInSp("cb1",isChecked);
break;
case R.id.cb2:
saveInSp("cb2",isChecked);
break;
case R.id.cb3:
saveInSp("cb3",isChecked);
break;
case R.id.cb4:
saveInSp("cb4",isChecked);
break;
case R.id.rb1:
saveInSp("rb1",isChecked);
break;
}
}
}
Now my code look like ( still no works ): 现在我的代码看起来像(仍然没有用):
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
int numberOfTrue = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox cb1,cb2,cb3,cb4;
Button b2;
TextView tv1;
b2 = (Button)findViewById(R.id.b2);
tv1 = (TextView)findViewById(R.id.tv1);
cb1 = (CheckBox)findViewById(R.id.cb1);
cb1.setChecked(getFromSP("cb1"));
cb1.setOnCheckedChangeListener(this);
cb2 = (CheckBox)findViewById(R.id.cb2);
cb2.setChecked(getFromSP("cb2"));
cb2.setOnCheckedChangeListener(this);
cb3 = (CheckBox)findViewById(R.id.cb3);
cb3.setChecked(getFromSP("cb3"));
cb3.setOnCheckedChangeListener(this);
cb4 = (CheckBox)findViewById(R.id.cb4);
cb4.setChecked(getFromSP("cb4"));
cb4.setOnCheckedChangeListener(this);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
});
String name = String.valueOf(numberOfTrue);
tv1.setText(name);
}
private boolean getFromSP(String key){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.cb1:
saveInSp("cb1",isChecked);
if (isChecked == true){
numberOfTrue++;
}
break;
case R.id.cb2:
saveInSp("cb2",isChecked);
if (isChecked == true){
numberOfTrue++;
}
break;
case R.id.cb3:
saveInSp("cb3",isChecked);
if (isChecked == true){
numberOfTrue++;
}
break;
case R.id.cb4:
saveInSp("cb4",isChecked);
if (isChecked == true){
numberOfTrue++;
}
break;
}
}
}
You can launch the second activity with startActivityForResult()
and get the callback to update the text on the first activity. 您可以使用
startActivityForResult()
启动第二个活动,并获取回调以更新第一个活动的文本。 More here: startActivityForResult on Android docs 此处更多内容: Android文档上的startActivityForResult
private void saveNumber(String key,int value) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
Simply save the number of trues you do if you're already using shared prefs, 如果您已经在使用共享偏好设置,则只需保存正确的数目,
in the onCheckedChanged if the boolean is true add 1 and if it's false remove 1 , and ofcourse initial at 0. 在onCheckedChanged中,如果布尔值为true,则添加1,如果为false,则删除1,当然,其初始值为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.