简体   繁体   English

当我单击edittextpreference时,EditText输入没有出现

[英]EditText input doesn't appear when I click in an edittextpreference

I'm using editTextPreference to implement a Setting Activity. 我正在使用editTextPreference来实现Setting Activity。 My problem is that when I click in an EditTextPreference it displays a dialog with the preference title but not appear an edittext to write. 我的问题是,当我单击EditTextPreference时,它将显示一个带有首选项标题的对话框,但没有出现要编写的edittext。 I have searched for this problem but It seems it hasn't happened to anyone. 我已经搜索了这个问题,但是似乎任何人都没有发生过。 Any help would be appreciated 任何帮助,将不胜感激

I have this code : 我有这个代码:

@EActivity
@OptionsMenu(R.menu.settings_activity_menu)
public class ActivitySettings extends Activity
{

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);

       // Display the fragment as the main content.
       getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new FragSettings_())
            .commit();
   }
}



@EFragment
public class FragSettings extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener 
{
    @App
    MyApplication app;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);


    }

    @AfterViews
    void init()
    {
       EditTextPreference url =(EditTextPreference)findPreference(app.getResource(R.string.CONFIG_URL_SERVER));
        EditTextPreference user =(EditTextPreference)findPreference(app.getResource(R.string.CONFIG_USER_);

        if(url!=null)
            url.setSummary(app.getSettings().getSharedPreferences().getString(app.getResource(R.string.CONFIG_URL_SERVER), ""));

        if(user!=null)
            user.setSummary(app.getSettings().getSharedPreferences().getString(app.getResource(R.string.CONFIG_USER_), ""));

    }

   @Override
   public void onResume() {
       super.onResume();
       // Register as listener to validate settings values
       app.getSettings().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
   }

   @Override
   public void onPause() {
       super.onPause();
       // Remove listener
       app.getSettings().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
   }

   @Override
   public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        EditTextPreference connectionPref = (EditTextPreference) findPreference(key);

           // Set summary to be the user-description for the selected value
           connectionPref.setSummary(app.getSettings().getSharedPreferences().getString(key, ""));
    }


}

My preferences.xml, in these editTextPreference is where I have the problem, the input doesn't appear : 我的preferences.xml,在这些editTextPreference中是我遇到问题的地方,输入未出现:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
    android:title="@string/settings_server">

    <EditTextPreference
        android:key="@string/CONFIG_USER_"
        android:title="@string/settings_user"/>

    <EditTextPreference
        android:inputType="textPassword"
        android:key="@string/CONFIG_PASS_"
        android:title="@string/settings_password"/>

    <EditTextPreference
        android:key="@string/CONFIG_URL_SERVER"
        android:title="@string/settings_url"/>

</PreferenceCategory>

<Preference
        android:key="@string/AREA"
        android:title="@string/settings_area"/>

</PreferenceScreen>

At the end I found what was happening. 最后,我发现了正在发生的事情。 I wanted to use the same text style that I was using in all my application, so in ManifestFIle.xml I had this: 我想使用与所有应用程序中相同的文本样式,因此在ManifestFIle.xml中,我拥有以下样式:

<activity
        android:name=".mobile.settings.ActivitySettings_"
        android:theme="@style/PreferenceActivity"
        android:label="@string/title_activity_settings"/>

Which refers to this style: 指的是这种风格:

<style name="PreferenceActivity" parent="AppTheme">
    <item name="android:editTextPreferenceStyle">@style/CodeFont</item>
    <item name="android:preferenceCategoryStyle">@style/CodeFont</item>
</style>

<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">@dimen/text_size_medium</item>
</style>

It seems that if I use editTextPreferenceStyle it makes disappear the box input. 看来,如果我使用editTextPreferenceStyle,它会使框输入消失。 This is my solution: 这是我的解决方案:

<activity
        android:name=".mobile.settings.ActivitySettings_"
        android:theme="@style/AppTheme"

        android:label="@string/title_activity_settings"/>

And to custom the edit text I use this http://secutyhf.org/wordpress/zeegers/2014/12/16/android-edittextpreference-style/ 为了自定义编辑文本,我使用以下http://secutyhf.org/wordpress/zeegers/2014/12/16/android-edittextpreference-style/

<style name="AppTheme" parent="android:Theme.Holo">
    <item name="android:editTextStyle">@style/EdittEXTsTYLE</item>
</style>

<style name="EdittEXTsTYLE" parent="android:Widget.EditText">

</style>

What I really not understand is Your Parameters, but let me suspect You are giving the correct references with these to the Preferences. 我真正不了解的是您的参数,但让我怀疑您是否在使用这些参数正确引用了首选项。 But usually, You have to initialize them with the correct Object: 但是通常,您必须使用正确的Object对其进行初始化:

    Preference url =findPreference(Parameters.CONFIG_URL_SERVER);
    Preference user =findPreference(Parameters.CONFIG_USER_);

must be 一定是

    EditTextPreference url =(EditTextPreference)findPreference(Parameters.CONFIG_URL_SERVER);
    EditTextPreference user =(EditTextPreference)findPreference(Parameters.CONFIG_USER_);

And it´s better to set the keys inside Your xml layout as a reference to a string in strings.xml, for example: 并且最好将xml布局内的键设置为对strings.xml中字符串的引用,例如:

 <EditTextPreference
    android:key="@string/user_pref_key"
    android:title="@string/settings_user"/>

EDIT 编辑

Be sure to use the correct context. 确保使用正确的上下文。 For this, You have to wait until the Activity is attached by Override onAttachActivity. 为此,您必须等到活动被Override onAttachActivity附加。 Make a global Context: 建立全局上下文:

  private Context mContext;

     @Override
      public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
          super.onAttach(activity);
          mContext=activity;
     }

Then You can refer it in Your PreferenceFragment. 然后,您可以在您的PreferenceFragment中引用它。

  EditTextPreference url =(EditTextPreference)findPreference(mContext.getResources().getString(R.string.user_pref_key));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM