0

我想从我的edittextpreference中获取值,并在我的mainActivity布局中更改textviews的字体大小。请帮助我与这些家伙。我一定在做一些愚蠢的错误。帮助将不胜感激。如何在我的MainActivity中获取EditTextPreference的引用?

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> 
+0

SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(本); String str = sp.getString(“editTextPref”,“”); –

+0

我希望这会帮助你尝试这一个。 –

+0

没有问题是关键...仍然感谢replyng兄弟:) – Rahul

回答

3

你应该阅读的关键fontSize不是的EditTextPreference id字段的名称值。更改您的密码在你的MainActivity这样的:

SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this); 
String str=sp.getString("fontSize","50"); 
+0

是兄弟比...我认为editTextPref是我的钥匙,并试图与那onyl ...后你回答我再次检查我的pref.xml并抨击我的头哈哈。谢谢。 – Rahul

+0

非常感谢@mirekkomis。 –

相关问题