我有一个类,我已将其定义为 EditText 的扩展/我用它来解决我们一直遇到的问题,因为没有出现右对齐提示。因此,我编写了自己的代码,在内部实现了提示。
其中,我重写了 getText 方法,以便在值等于提示的情况下返回 null。问题是 getText 被不停地调用。它不是递归调用的。我在那里放了一个计数器,它肯定会在执行下一个呼叫之前退出一个呼叫。
@Override
public Editable getText(){
depth++;
calls++;
Log.d("RightJustifiedEditText","getText - start. Depth = " + depth + " calls = " + calls);
Editable result = (isEmpty?Editable.Factory.getInstance().newEditable(hint):super.getText());
depth--;
Log.d("RightJustifiedEditText","getText - return: " + result + " Depth = " + depth);
return result;
}
当我运行它时,“深度”的值每次显示为 1,然后显示为 0。“电话”的价值不断上升。
如果我删除“super.getText()”并在其位置放置一个常量,那么它似乎没问题。
从我在调试跟踪中可以看到,它是从布局中调用的:
RightJustifiedEditText.getText() line: 115
RightJustifiedEditText(EditText).getText() line: 48
RightJustifiedEditText(TextView).getSelectionStart() line: 5892
RightJustifiedEditText(TextView).onDraw(Canvas) line: 4057
RightJustifiedEditText(View).draw(Canvas) line: 6880
RelativeLayout(ViewGroup).drawChild(Canvas, View, long) line: 1646
RelativeLayout(ViewGroup).dispatchDraw(Canvas) line: 1373
TextFieldElement(ViewGroup).drawChild(Canvas, View, long) line: 1644
TextFieldElement(ViewGroup).dispatchDraw(Canvas) line: 1373
LinearLayout(ViewGroup).drawChild(Canvas, View, long) line: 1644
LinearLayout(ViewGroup).dispatchDraw(Canvas) line: 1373
DoctorSearchElement(ViewGroup).drawChild(Canvas, View, long) line: 1644
DoctorSearchElement(ViewGroup).dispatchDraw(Canvas) line: 1373
LinearLayout(ViewGroup).drawChild(Canvas, View, long) line: 1644
LinearLayout(ViewGroup).dispatchDraw(Canvas) line: 1373
ScrollView(ViewGroup).drawChild(Canvas, View, long) line: 1644
ScrollView(ViewGroup).dispatchDraw(Canvas) line: 1373
ScrollView(View).draw(Canvas) line: 6986
ScrollView(FrameLayout).draw(Canvas) line: 357
ScrollView.draw(Canvas) line: 1409
RelativeLayout(ViewGroup).drawChild(Canvas, View, long) line: 1646
RelativeLayout(ViewGroup).dispatchDraw(Canvas) line: 1373
RelativeLayout(View).draw(Canvas) line: 6883
FrameLayout(ViewGroup).drawChild(Canvas, View, long) line: 1646
FrameLayout(ViewGroup).dispatchDraw(Canvas) line: 1373
FrameLayout(View).draw(Canvas) line: 6883
FrameLayout.draw(Canvas) line: 357
LinearLayout(ViewGroup).drawChild(Canvas, View, long) line: 1646
LinearLayout(ViewGroup).dispatchDraw(Canvas) line: 1373
PhoneWindow$DecorView(ViewGroup).drawChild(Canvas, View, long) line: 1644
PhoneWindow$DecorView(ViewGroup).dispatchDraw(Canvas) line: 1373
PhoneWindow$DecorView(View).draw(Canvas) line: 6883
PhoneWindow$DecorView(FrameLayout).draw(Canvas) line: 357
PhoneWindow$DecorView.draw(Canvas) line: 1862
ViewRoot.draw(boolean) line: 1522
ViewRoot.performTraversals() line: 1258
ViewRoot.handleMessage(Message) line: 1859
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]
这是直接包含它的布局:
<?xml version="1.0" encoding="UTF-8"?>
<com.applicat.meuchedet.views.RightJustifiedEditText
android:id="@+id/text_field_element_text_field"
android:layout_width="170dip"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:textColor="@color/text_field_color"
android:textSize="14sp"
android:layout_marginRight="5dip"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:gravity="right|center_vertical"
android:inputType="text"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:background="@drawable/text_field_bg"
/>
<TextView
android:id="@+id/text_field_element_prompt"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:gravity="center_vertical|right"
android:layout_centerVertical="true"
android:textSize="16sp"
android:singleLine="true"
/>