在我的设置活动中,用户可以更改其名称。我想做的是,当用户更改名称时,我想获取该数据并将另一个活动中的文本设置为新名称。
在我的设置活动中,我有这段代码:
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;
}
};
答案 0 :(得分:0)
如果将值存储在“共享首选项”中,则可以在需要获取新数据的活动中使用OnSharedPreferencesChangedListener
。 https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener
在onResume
和onCreate
中注册到此侦听器。
另一个选择是保存到数据库。然后,您可以使用Room库返回LiveData对象。如果更改了数据,LiveData将自动更新屏幕。
答案 1 :(得分:0)
Room temp = new Room(RoomItems.WALL, RoomItems.START, RoomItems.LOCK );