【问题标题】:SharedPreferences EditText dialog squashed on HTC WildFireSharedPreferences EditText 对话框在 HTC WildFire 上被压扁
【发布时间】:2011-05-22 09:41:57
【问题描述】:

我在我的 Android 应用程序中以标准方式使用 SharedPreferences。在 HTC WildFire 设备(分辨率 240x320)上,显示虚拟键盘时 EditText 被压扁。

有没有其他人遇到过这个有解决方案吗?这几天我都被难住了。

我的代码/XML 非常简单:

public class PrefsActivity extends PreferenceActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    // don't display hidden preferences
    getPreferenceScreen().removePreference(findPreference("hidden_prefs"));
}
}

还有我的preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="User Preferences">   
    <EditTextPreference
        android:key="profile"
        android:title="Profile"
        android:summary="Your profile name"
        android:name="Profile"/>

    <EditTextPreference
        android:key="password"
        android:title="Password"
        android:summary="Your password"
        android:name="Password"
        android:password="true"/>

    <EditTextPreference
        android:name="Server"
        android:summary="The server to use"
        android:title="Server"
        android:key="server"/>

    <EditTextPreference
        android:name="Secret"
        android:summary="The server secret"
        android:title="Secret"
        android:password="true"
        android:key="secret"/>

    <CheckBoxPreference
        android:title="On Demand"
        android:defaultValue="true"
        android:summary="Check to enable On Demand"
        android:key="on_demand"/>

    <ListPreference
        android:title="Date"
        android:summary="Set the type of date used"
        android:key="date"
        android:defaultValue="next"
        android:entries="@array/prefs_date_keys"
        android:entryValues="@array/prefs_date_values" />

</PreferenceCategory>

<PreferenceCategory android:key="hidden_prefs" android:title="Hidden Preferences">

    <EditTextPreference
        android:name="Last Project ID"
        android:summary="The last Project ID"
        android:title="Last Project ID"
        android:key="last_project_id"
        android:inputType="number"/>

    <EditTextPreference
        android:name="Fast Sync"
        android:summary="Use Fast Sync"
        android:title="Fast Sync"
        android:key="fast_sync"
        android:inputType="number"/>

</PreferenceCategory>

【问题讨论】:

    标签: android sharedpreferences htc-android


    【解决方案1】:

    我知道这个答案很可能来得太晚了,但如果其他人正在寻找这个真正烦人的问题的解决方案,我就是这样解决它的,试图让它尽可能地保持原样(对我来说,自定义在这种情况下,布局是绝对禁止的):

    public class MyEditTextPreference extends EditTextPreference
    {
        public MyEditTextPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void showDialog(Bundle bundle) {
            super.showDialog(bundle);
    
            Dialog dialog = getDialog();
            if(dialog != null) {
                Window window = dialog.getWindow();
                window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE |
                    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            }
        }
    }
    

    使用此代码,屏幕键盘将显示在对话框按钮的顶部,但 EditText 仍然可见。

    【讨论】:

      【解决方案2】:

      两个建议: 1)更简单的方法可能会或可能不会给您足够的空间,因为您的键盘看起来比标准的 android 键盘高:在首选项活动中隐藏应用程序标题栏和 android 状态栏。这会将对话框向上移动一点。

      @Override
      public void onCreate(Bundle savedInstanceState) {
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
          super.onCreate(savedInstanceState);
      

      2) 更难的方法是通过扩展 DialogPreference 或 EditTextPreference 来编写您自己的自定义首选项控件,并为该控件定义您自己的布局,该控件具有较小或没有标题,或者可能较小的确定/取消按钮或其他东西。然后将该自定义控件放入您的preferences.xml

      【讨论】:

      • 感谢您的建议。我不想破坏应用程序的外观和感觉(特别是对于 HTC WildFire 以外的手机),所以我想我会选择 2)。您能否提供有关如何执行此操作的参考?我认为更改标题的字体大小或填充将是可行的方法。
      【解决方案3】:

      经过几次不同的尝试后,以下代码运行良好(带有编辑框的对话框不太好,但它很实用)。应自定义 EditTextPreference(也应更改具有首选项的资源文件)。 CustomTextPreference 应该有 3 个简单的构造函数。

      公共类 CustomEditTextPreference 扩展 EditTextPreference { ...

      @Override
      protected void showDialog(Bundle state)
      {
          super.showDialog(state);
      
          // solving problem with insufficient space for showing EditText in EditTextPreference's dialog. This problem exists only
          // on HTC WildFire with android OS 2.2
          if ( Build.MODEL.equals("HTC Wildfire") && 8 == Integer.valueOf(android.os.Build.VERSION.SDK) )
          {
              _makeMoreRoomForEditText();
          }
      
      }
      
      private void _makeMoreRoomForEditText()
      {
          EditText editText = getEditText();
      
          View parent1 = (View) editText.getParent();
          View parent2 = (View) parent1.getParent();
          View parent3 = (View) parent2.getParent();
          View parent4 = (View) parent3.getParent();
          View parent5 = (View) parent4.getParent();
      
          editText.setPadding(editText.getPaddingLeft(), 0, editText.getPaddingRight(), 0);
          parent1.setPadding(parent1.getPaddingLeft(), 0, parent1.getPaddingRight(), 0);
          parent3.setPadding(parent1.getPaddingLeft(), 0, parent1.getPaddingRight(), 0);
          parent5.setPadding(parent5.getPaddingLeft(), 0, parent5.getPaddingRight(), 0);
      
          ViewGroup par5 = (ViewGroup) parent5;
          View v = par5.getChildAt(0);
      
          v.setPadding(v.getPaddingLeft(), 3, v.getPaddingRight(), 3);// empirically determined values - looks fine only on HTC WildFire device with 2.2 Android
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        相关资源
        最近更新 更多