我手动创建了一个偏好设置活动,其中复选框数据将被发送到另一个活动。当我发送它时,它是对的。但是当我收到它时,我需要知道它是真还是假,而且我总是弄错。
这是我发送的代码:
@Override
public void onBackPressed() {
if(checkBox.isChecked())
{
String trueThumb_check = "checked_thumb";
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.putExtra("trueThumbnail", trueThumb_check);
Toast.makeText(Preferences.this, "check prefs", Toast.LENGTH_SHORT).show();
startActivity(i);
killAc();
}
else
{
String falseThumb_check = "not_check_thumb";
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.putExtra("trueThumbnail", falseThumb_check);
Toast.makeText(Preferences.this, "not check prefs", Toast.LENGTH_SHORT).show();
startActivity(i);
killAc();
}
}
接收:
try {
SharedPreferences thumb_check = this.getSharedPreferences(
"ch4an.ytheloader", Context.MODE_PRIVATE);
//To read preferences
String thumb_thumb = thumb_check.getString("trueThumbnail", "checked_thumb");
if (thumb_thumb.contains("checked_thumb")){
Toast.makeText(MainActivity.this, "checkBox ativo", Toast.LENGTH_SHORT).show();
} if(thumb_thumb.contains("not_checked_thumb")) {
Toast.makeText(MainActivity.this, "checkBox não ativo", Toast.LENGTH_SHORT).show();
}
SharedPreferences.Editor editor = thumb_check.edit();
editor.apply();
} catch (Throwable e) {
Toast.makeText(MainActivity.this, "Null checkBox", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
我认为问题可能在这里:
String thumb_thumb = thumb_check.getString("trueThumbnail", "checked_thumb");
我总是得到一个“checked_thumb”,但我不知道如何获得“thumb_thumb”。 我尝试发送不同的键和值(因为两者都在“trueThumbnail”键下)但我仍然只是真实。
答案 0 :(得分:0)
如果您不知道如何用英语提问,请访问pt.stackoverflow.com
但是因为我是葡萄牙语,我会在这里回答,但是用英语回答。
您的接收代码有误。当您发送通过意图发送的数据时,那里没有任何问题,但是当您尝试接收时,您将从SharedPreferences中获取
所以要访问数据而不是这个
SharedPreferences thumb_check = this.getSharedPreferences(
"ch4an.ytheloader", Context.MODE_PRIVATE);
应该是这个
Intent thumb_check = getIntent();
String thumb_thumb = thumb_check.getStringExtra("trueThumbnail", "checked_thumb");
重写接收代码
try {
// Get the intent passed through activities
Intent thumb_check = getIntent();
// Get the string in the extra
String intent_Thumb = thumb_check.getStringExtra("trueThumbnail", "checked_thumb");
// Get the shared preferences
SharedPreferences prefs = this.getSharedPreferences(
"ch4an.ytheloader", Context.MODE_PRIVATE);
//To read from preferences
String pref_Thumb = prefs("trueThumbnail", "checked_thumb");
// preform what ever you want
} catch (Throwable e) {
Toast.makeText(MainActivity.this, "Null checkBox", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
您正在混合SharedPreferences和Intents,似乎您迷失在该代码中 一定要阅读它帮助很多的android文档。 https://developer.android.com/guide/components/intents-filters.html
编辑回复您的评论
SharedPreferences sharedPref = getSharedPreferences("ch4an.ytheloader", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("trueThumbnail", intent_Thumb);
// Or if you want to put a bool assuming that you have a
// variable called intent_Thumb_as_boolean as a boolean
editor.putBoolean("trueThumbnail", intent_Thumb_as_boolean);
editor.commit();
检查Android文档中的SharedPreferences
https://developer.android.com/training/data-storage/shared-preferences.html