EditTextを拡張して他のビューを重ねて表示できるようにするための良い解決方法を見つけようとしています。私は文字の数を表示するために上部にTextViewを持っているEditTextであるカスタムビューを作成しようとしています。クリアボタンの上部にはImageViewがあります。Android - カスタムビュー - 上にビューを持つEditTextを拡張する
現在、私はFrameLayoutを拡張することに取り組んでいますが、それは私が探しているコントロール/柔軟性を与えません。私はButterKnifeの@OnTextChangedをTextViewを期待して使用することはできません。私はそれらを渡さない限りEditTextのXML属性に直接アクセスすることはできません。
お時間をいただきありがとうございます。
はClearableEditText.java
public class ClearableEditText extends FrameLayout {
public ClearableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
protected void init() {
View inflate = inflate(getContext(), R.layout.clearable_edit_text, this);
ButterKnife.bind(this, inflate);
...
}
...
}
R.layout.clearable_edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/clearable_edit"
style="@style/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<ImageView
android:id="@+id/clearable_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:src="@drawable/button_clear"
android:visibility="invisible"
tools:visibility="visible" />
<TextView
android:id="@+id/clearable_count"
style="@style/edit_text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
tools:text="1200" />
</FrameLayout>
このレイアウトを使用し、テキストを編集してテキストビューに数字を表示するだけで、同じ機能を実現することはできませんか?編集テキストをクリアするのと同じことです。ここでのButterKnifeの使用法は過度の冗談だと私には思われます。そうでなければ整理できる機能のカスタムビューを作成しています。私が何かを見逃しているなら、遠くに行く理由を理解したいと思います。 – daxgirl
私はを使ってレイアウト内に持っていましたが、問題はこの機能がすべて私のアプリケーション上にあったことです。私は1つのクリーンなカスタムビューにすることによってカウンタ/クリアを処理するためのすべての余分なコードを再現しようとしています。 –