如何在我的 MainActivity 中获取 EditTextPreference 的引用?
How To Get Reference of EditTextPreference in my MainActivity?
我正在尝试从我的 edittextpreference 中获取值并更改我的 mainActivity 布局中文本视图的字体大小。请帮我解决这个 guys.I 一定是在做一些傻事 mistake.Help 将不胜感激。
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this);
String str=sp.getString("editTextPref","50");
if(sp!=null)
{
Toast.makeText(this,"String value is "+str,Toast.LENGTH_LONG).show();
txt=(TextView) findViewById(R.id.textView);
txt.setTextSize(Float.parseFloat(str));
}
Toast.makeText(this,"String value is :"+str,Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int items=item.getItemId();
switch(items)
{
case R.id.setting:
Toast.makeText(this,"Settings Clicked",Toast.LENGTH_LONG).show();
finish();
Intent i=new Intent(this,Prefs.class);
startActivity(i);
break;
}
return super.onOptionsItemSelected(item);
}
}
Prefs.java
public class Prefs extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
// show the current value in the settings screen
for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
initSummary(getPreferenceScreen().getPreference(i));
}
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePreference(findPreference(key));
}
private void initSummary(Preference p) {
if (p instanceof PreferenceCategory) {
PreferenceCategory cat = (PreferenceCategory) p;
for (int i = 0; i < cat.getPreferenceCount(); i++) {
initSummary(cat.getPreference(i));
}
} else {
updatePreference(p);
}
}
private void updatePreference(Preference p)
{
if(p instanceof EditTextPreference)
{
EditTextPreference editTextPreference=(EditTextPreference) p;
p.setSummary(editTextPreference.getText());
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
startActivity(new Intent(Prefs.this,MainActivity.class));
Toast.makeText(this,"Pref activity finished",Toast.LENGTH_LONG).show();
}
}
Prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:enabled="false"
android:key="settingCategory"
android:title="Settings"
>
</PreferenceCategory>
<EditTextPreference
android:id="@+id/editTextPref"
android:dialogMessage="I am Dialog Message"
android:dialogTitle="Dialog Title"
android:hint="e.g 25"
android:icon="@mipmap/ic_launcher"
android:key="fontSize"
android:padding="5dp"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="Changes Font Size of Overall App"
android:title="Change Font Size" />
</PreferenceScreen>
您应该读取键 fontSize
的值,而不是 EditTextPreference
的 ID 字段的名称。像这样更改 MainActivity
中的代码:
SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this);
String str=sp.getString("fontSize","50");
我正在尝试从我的 edittextpreference 中获取值并更改我的 mainActivity 布局中文本视图的字体大小。请帮我解决这个 guys.I 一定是在做一些傻事 mistake.Help 将不胜感激。
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this);
String str=sp.getString("editTextPref","50");
if(sp!=null)
{
Toast.makeText(this,"String value is "+str,Toast.LENGTH_LONG).show();
txt=(TextView) findViewById(R.id.textView);
txt.setTextSize(Float.parseFloat(str));
}
Toast.makeText(this,"String value is :"+str,Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int items=item.getItemId();
switch(items)
{
case R.id.setting:
Toast.makeText(this,"Settings Clicked",Toast.LENGTH_LONG).show();
finish();
Intent i=new Intent(this,Prefs.class);
startActivity(i);
break;
}
return super.onOptionsItemSelected(item);
}
}
Prefs.java
public class Prefs extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
// show the current value in the settings screen
for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
initSummary(getPreferenceScreen().getPreference(i));
}
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePreference(findPreference(key));
}
private void initSummary(Preference p) {
if (p instanceof PreferenceCategory) {
PreferenceCategory cat = (PreferenceCategory) p;
for (int i = 0; i < cat.getPreferenceCount(); i++) {
initSummary(cat.getPreference(i));
}
} else {
updatePreference(p);
}
}
private void updatePreference(Preference p)
{
if(p instanceof EditTextPreference)
{
EditTextPreference editTextPreference=(EditTextPreference) p;
p.setSummary(editTextPreference.getText());
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
startActivity(new Intent(Prefs.this,MainActivity.class));
Toast.makeText(this,"Pref activity finished",Toast.LENGTH_LONG).show();
}
}
Prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:enabled="false"
android:key="settingCategory"
android:title="Settings"
>
</PreferenceCategory>
<EditTextPreference
android:id="@+id/editTextPref"
android:dialogMessage="I am Dialog Message"
android:dialogTitle="Dialog Title"
android:hint="e.g 25"
android:icon="@mipmap/ic_launcher"
android:key="fontSize"
android:padding="5dp"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="Changes Font Size of Overall App"
android:title="Change Font Size" />
</PreferenceScreen>
您应该读取键 fontSize
的值,而不是 EditTextPreference
的 ID 字段的名称。像这样更改 MainActivity
中的代码:
SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this);
String str=sp.getString("fontSize","50");