java - 如何从设置活动中获取字符串数据并将其发送到另一个活动?
问题描述
在我的设置活动中,用户可以更改他们的姓名。我想要做的是当用户更改他们的名字时,我想获取该数据并将另一个活动中的文本设置为新名称。
在我的设置活动中,我有这段代码:
if (preference instanceof EditTextPreference) {
if (preference.getKey().equals("key_full_name")) {
// update the changed name to summary filed
preference.setSummary(stringValue);
Intent returnIntent = new Intent();
returnIntent.putExtra("new_Name", stringValue);
setResult(Activity.RESULT_OK,returnIntent);
}
}
stringValue
具有存储新名称的数据。当用户单击设置屏幕左上角的后退按钮时,我想要它,另一个活动将文本设置为新名称。
这是显示文本的其他活动的 xml 文件:
<TextView
android:id="@+id/myName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_full_name"
android:textSize="35sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.358" />
编辑:包括 AppCompatPreferenceActivity.java 文件
package app.debata.com.debata;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
* to be used with AppCompat.
*/
public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
@Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
public ActionBar getSupportActionBar() {
return getDelegate().getSupportActionBar();
}
public void setSupportActionBar(@Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
@Override
public MenuInflater getMenuInflater() {
return getDelegate().getMenuInflater();
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
@Override
public void setContentView(View view) {
getDelegate().setContentView(view);
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().setContentView(view, params);
}
@Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().addContentView(view, params);
}
@Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
@Override
protected void onTitleChanged(CharSequence title, int color) {
super.onTitleChanged(title, color);
getDelegate().setTitle(title);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getDelegate().onConfigurationChanged(newConfig);
}
@Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
public void invalidateOptionsMenu() {
getDelegate().invalidateOptionsMenu();
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
}
编辑:在 settings.java 文件中包含上述 if 语句的整个方法
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String stringValue = newValue.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(R.string.default_ringtone);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else if (preference instanceof EditTextPreference) {
if (preference.getKey().equals("key_full_name")) {
// update the changed name to summary filed
preference.setSummary(stringValue);
Intent returnIntent = new Intent();
returnIntent.putExtra("new_Name", stringValue);
setResult(Activity.RESULT_OK,returnIntent);
} else if (preference.getKey().equals("key_username")) {
// update the changed gallery name to summary filed
preference.setSummary(stringValue);
} else if (preference.getKey().equals("key_birthday")) {
// update the changed gallery name to summary filed
preference.setSummary(stringValue);
} else if (preference.getKey().equals("key_email")) {
// update the changed gallery name to summary filed
preference.setSummary(stringValue);
}
} else {
preference.setSummary(stringValue);
}
return true;
}
};
解决方案
如果您将值存储在共享首选项中,您可以OnSharedPreferencesChangedListener
在需要获取新数据的活动中使用。https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListeneronResume
在onCreate
.
_
另一种选择是保存到数据库。然后您可以使用返回 LiveData 对象的 Room 库。如果数据发生更改,LiveData 将自动更新屏幕。
推荐阅读
- apache-camel - 骆驼 3.2 和 Javascript
- javascript - 云IDE(MERN Stack)上的React API通信拒绝
- macos - 为什么我在尝试安装 python 包时总是遇到权限问题?
- r - 从 sqlite 将非常大的数据集导入到 h2o
- excel - 选中和取消选中复选框
- postgresql - Debezium postgres kafka 连接器因 Java 堆空间问题而失败
- mysql - 使用 MySQL 在 JSON 与行/列中存储图像路径?
- amazon-web-services - 为什么我不能通过 cognito UserInfo 端点从谷歌提供商那里获得更多属性?
- unit-testing - 有没有办法通过 NumberFormat 比较格式化值?
- python - 在使用 python 调试器进行表达式评估的底层?