【发布时间】:2015-07-27 02:24:17
【问题描述】:
我创建了一个扩展 EditText 的自定义小部件,它看起来像 EditText,但行为有点不同,这是代码,虽然我不认为该类的内容是问题:
public class SalutationEditTextLikeButton extends EditText {
CharSequence[] options;
String selection;
Context context;
long performClickCatch = 0;
public void dontPerformClickForMilliseconds(long milliseconds) {
performClickCatch = System.currentTimeMillis() + milliseconds;
}
public SalutationEditTextLikeButton(Context context) {
super(context);
this.context = context;
disableInput();
}
public SalutationEditTextLikeButton(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
this.context = context;
disableInput();
}
public SalutationEditTextLikeButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
disableInput();
}
private void disableInput() {
this.setCursorVisible(false);
setKeyListener(null);
}
public String getSelection() {
return selection;
}
public void setSelection(String selection) {
this.selection = selection;
this.setHint("");
this.setText(selection);
this.clearComposingText();
}
public void setOptions(CharSequence[] options) {
this.options = options;
}
@Override
public Parcelable onSaveInstanceState() {
Log.d("butx", "save");
return super.onSaveInstanceState();
}
@Override
public boolean performClick() {
if (System.currentTimeMillis() < performClickCatch) {
return true;
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getResources().getString(R.string.anrede));
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setSelection(options[which].toString());
setError(null);
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
this.clearComposingText();
return true;
}
}
我这样使用这个类:
final SalutationEditTextLikeButton salutationEditTextLikeButton
= (SalutationEditTextLikeButton) LayoutInflater.from(context).inflate(R.layout.single_salutationbutton_w_style, null);
xml 布局为:
<xy.SalutationEditTextLikeButton
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
在线上
final SalutationEditTextLikeButton salutationEditTextLikeButton
= (SalutationEditTextLikeButton) LayoutInflater.from(context).inflate(R.layout.single_salutationbutton_w_style, null);
我收到一份关于 Crittercism 的罕见异常报告,但有以下异常:
Caused by: java.lang.ClassCastException:
xy.SalutationEditTextLikeButton cannot be cast to xy.SalutationEditTextLikeButton
这对我来说似乎很奇怪。 A 偶尔与其他自定义小部件有相同的异常。
这仅出现在三星的 Galaxy S5 (SM-G900F) 上。
关于这意味着什么以及如何解决它的任何想法?
【问题讨论】:
-
您可能有一份 SalutationEditTextLikeButton.java 的副本?
标签: android android-edittext android-widget