![](/img/trans.png)
[英]how i can get onActivityResult in fragment from secondActivity
[英]how to get value of switch button from secondActivity?
在我的项目中,我有2个活动,在第二个活动中,我有一个开关按钮。
我有2个问题:
1:我想从第二个活动中获取切换按钮的值,并通过使用共享的首选项在第一个活动中显示吐司,将其传递给第一个活动,但是我没有得到切换按钮的值,因此吐司无法正常工作。
2:当我按返回或关闭应用程序时,开关按钮的值更改为默认值,如何保存开关按钮的值?
这是我的第一个活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Boolean isChecked = settings.getBoolean("status" , false);
if (isChecked){
Toast.makeText(this, "Enabled", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Disabled", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_setting:
Intent settingIntent = new Intent(getApplicationContext(), Main2Activity.class);
startActivity(settingIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的第二项活动:
public class Main2Activity extends AppCompatActivity {
Switch aSwitch;
EditText editText;
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editText = (EditText) findViewById(R.id.editText);
aSwitch = (Switch) findViewById(R.id.switch1);
back = (Button) findViewById(R.id.btnBack);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent swIntent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(swIntent);
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("MySwitch", true);
super.onSaveInstanceState(outState);
}
public void enable_disable(View view) {
if (aSwitch.isChecked()) {
SharedPreferences.Editor editor = getSharedPreferences("switch", MODE_PRIVATE).edit();
editor.putBoolean("status", true);
editor.commit();
} else {
SharedPreferences.Editor editor = getSharedPreferences("switch", MODE_PRIVATE).edit();
editor.putBoolean("status", false);
editor.commit();
}
}
}
这是我第二项活动的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center"
tools:context="a.switchtest.Main2Activity">
<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="enable_disable"
android:text="Switch" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnBack"
android:text="Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
SharedPreferences只是存储在设备上的键值xml文件。 您遇到的问题是您使用的是两个不同的SharedPreferences,这就是为什么该值未显示的原因。
在您的SecondActivity
中,使用以下行:
getSharedPreferences("switch", MODE_PRIVATE).edit();
您正在创建一个名为switch.xml
的SharedPreferences文件,并将其中的开关值存储在其中。 但是,在您致电MainActivity
时:
PreferenceManager.getDefaultSharedPreferences(this);
这将获取“默认文件”,它是一个不同的xml文件,因此该值不存在。 您需要与两个活动之间要访问的SharedPreferences文件保持一致。 在这种情况下,我建议您仅使用默认文件并将您的SecondActivity
更改为:
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(this)
.edit();
editor.putBoolean("status", true);
editor.commit();
现在,该值将保存在默认文件中,该文件与您在MainActivity
读取的文件相同
使用开关onChangedListener。 这会将您的开关状态保留在Main2Activity中
public class Main2Activity extends AppCompatActivity {
Switch aSwitch;
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
aSwitch = (Switch) findViewById(R.id.switch1);
SharedPreferences sharedPreference = getSharedPreferences("switch", MODE_PRIVATE);
boolean isChecked = sharedPreference.getBoolean("status",false);
Log.d("Shriyansh",isChecked+"");
aSwitch.setChecked(isChecked);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked will be
// true if the switch is in the On position
SharedPreferences.Editor editor = getSharedPreferences("switch", MODE_PRIVATE).edit();
editor.putBoolean("status", isChecked);
editor.commit();
Log.d("Shriyansh1",isChecked+"");
}
});
back = (Button) findViewById(R.id.btnBack);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent swIntent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(swIntent);
}
});
}
现在,在您的第一个活动中显示吐司,您可以在MainActivity的onResume()方法中使用此首选项
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreference = getSharedPreferences("switch", MODE_PRIVATE);
boolean isChecked = sharedPreference.getBoolean("status",false);
if (isChecked){
Toast.makeText(this, "Enabled", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Disabled", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_setting:
Intent settingIntent = new Intent(getApplicationContext(),
Main2Activity.class);
startActivity(settingIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
您的Main2Activity的布局remove switch的enable_disable方法
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center"
tools:context="a.switchtest.Main2Activity">
<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnBack"
android:text="Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在您的MainActivity中,您需要更换
PreferenceManager.getDefaultSharedPreferences(this);
与
SharedPreferences sharedPreference = getSharedPreferences("switch", MODE_PRIVATE);
还要从您的Main2Activity中删除它
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("MySwitch", true);
super.onSaveInstanceState(outState);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.