我有一个从android 1.5到4.0都可以使用的应用程序。Android 4.1导致一个简单的EditTextPreference出现问题。该应用程序始终处于横向模式。当我提到团队名称时,它看起来是这样的。
图1http://myweb.midco.net/hgs/s1.jpg
然后,当我单击"编辑文本字段"时,它一直是这样的。
图1http://myweb.midco.net/hgs/s2.jpg
但在我的模拟器中,从三星Galaxy Tab 2与Android 4.1的投诉中,我得到了这个。
图1http://myweb.midco.net/hgs/s3.jpg
这会挤压文本字段。将清单更改为
android:windowSoftInputMode="stateVisible|adjustResize"
或
android:windowSoftInputMode="stateVisible|adjustPan"
没有起到任何作用。保留"标题"为空确实会删除标题,但您仍然看不到编辑文本。这是这个首选的代码
EditTextPreference teamnamePref = new EditTextPreference(this);
teamnamePref.setTitle("Team Name");
teamnamePref.setKey( "t" + Team_ID );
teamnamePref.setSummary(TheTeamName);
teamnamePref.setDialogTitle("Enter Name For Team " + Team_ID);
teamnamePref.setDefaultValue(TheTeamName);
teamnamePref.getEditText().setSingleLine(true);
TeamCategory.addPreference(teamnamePref);
感谢
我创建了一个方法来解决这个错误(在三星Galaxy S3、宏碁A501和HTC Desire X上测试):
// This method provides a work-around for a bug with EditTextPreference and the Samsung soft keyboard.
// When in landscape mode, the Samsung keyboard can obscure the text box, as it does not use fullscreen
// / extract mode like other keyboards do in this case. So we manually show an alert dialog with an
// EditText control, and update the preference value manually.
//
// Usage:
// Create a normal <Preference> in your XML file, then in your corresponding fragment, retrieve the
// preference object using its key and pass it to this method. E.g.
//
// EditTextPreferenceHelper.CreateEditTextPreferenceDialog(
// getActivity(),
// findPreference("yourPrefKey"));
public static void CreateEditTextPreferenceDialog(final Context context, Preference preference) {
preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(final Preference preference) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(preference.getTitle());
final EditText textBox = new EditText(context);
textBox.setImeOptions(EditorInfo.IME_ACTION_DONE);
textBox.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT);
textBox.setText(PreferenceManager.getDefaultSharedPreferences(context).getString(preference.getKey(), ""));
builder.setView(textBox);
builder.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PreferenceManager.getDefaultSharedPreferences(
context).edit().putString(preference.getKey(),
textBox.getText().toString()).commit();
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
return false;
}
});
}
应该这样做。您可以随时将软输入标志修改为其他内容,如adjustPan。这之前不起作用的原因是EditTextPreference覆盖了这些选项。
import android.content.Context;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.view.Window;
import android.view.WindowManager;
public class CustomEditTextPref extends EditTextPreference
{
public CustomEditTextPref(Context context)
{
super(context);
}
@Override
protected void showDialog(Bundle state)
{
super.showDialog(state);
Window window = getDialog().getWindow();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}