平板电脑设备上的android.widget.EditText投射问题

时间:2015-06-05 14:13:14

标签: android

Android 4.2.2 / Android Studio

它适用于手机设备。 仅在平板电脑设备上 我得到了:

java.lang.ClassCastException: android.widget.EditText cannot be cast to br.com.formshelper.EditTextHelper

创建主要活动 - 扩展活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    userHelper = new UserFormsHelperImpl(this);  /*cast exception here*/

USERFORMSHELPER

public class UserFormsHelperImpl {

private EditTextHelper userEMail;
private EditTextHelper userPassword;

public UserFormsHelperImpl(MainActivity activity) {
    this.userEMail = (EditTextHelper) activity.findViewById(R.id.editTextEmail);
    this.userPassword = (EditTextHelper) activity.findViewById(R.id.editTextSenha);

EDITTEXTHELPER CLASS

public class EditTextHelper extends EditText {

private KeyImeChange keyImeChangeListener;

public EditTextHelper(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setKeyImeChangeListener(KeyImeChange listener){
    keyImeChangeListener = listener;
}

public interface KeyImeChange {
    public void onKeyIme(int keyCode, KeyEvent event);
}

@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event){
    if(keyImeChangeListener != null){
        keyImeChangeListener.onKeyIme(keyCode, event);
    }
    return false;
}

活性

        userHelper.getUserEMail().setKeyImeChangeListener(new EditTextHelper.KeyImeChange() {
        @Override
        public void onKeyIme(int keyCode, KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                clearEditTextsFocus();
                setContentViewResized(false, false, true, true);
            }
        }
    });

XML

<br.com.formshelper.EditTextHelper
                android:layout_width="294dp"
                android:layout_height="38dp"
                android:paddingBottom="2dp"
                android:textSize="15dp" />

我认为有些东西与平板电脑上不存在的pysical后退按钮相关,并且确实存在于手机上。 但我不知道如何处理这个......

1 个答案:

答案 0 :(得分:1)

此异常说,找到了EditText并且您尝试将其强制转换为自定义视图 - 因此创建了EditText而不是自定义视图。

检查布局xml文件的替代方案以获得更大的屏幕 - 您可以在文件夹res / layout-sw720或res / layout-sw600中找到它们。您需要使用ids editTextEmail和editTextSenha将视图更改为自定义视图类。