2011-05-22 38 views
2

我在標準方式中使用我的Android應用程序中的SharedPreferences。在HTC WildFire設備(分辨率240x320)上,當顯示虛擬鍵盤時,EditText被壓扁。SharedPreferences EditText對話框在HTC WildFire上壓扁

有沒有其他人遇到過這個問題嗎?我被困了好幾天。

My SharedPreferences activity on the HTC WildFire

我的代碼/ 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> 

回答

1

兩個建議: 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

+0

感謝您的建議。我不想打破應用程序的外觀和感覺(特別是HTC WildFire以外的手機),所以我認爲我會選擇2)。你能提供一個關於如何做到這一點的參考?我認爲改變標題的字體大小或填充將是要走的路。 – 2011-05-22 11:50:59

2

我知道這個答案很可能來得太晚,但萬一其他人正在尋找解決這個真正煩人的問題,這是我如何解決它,試圖保持它作爲香草地(對我來說,自定義佈局是一個明確的禁忌在這種情況下):

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仍然可見。

1

下面的代碼的幾個不同的嘗試後效果很好(有編輯框對話框不是很好,但它的功能)。應該自定義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 
} 

}