0

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> 
+0

このレイアウトを使用し、テキストを編集してテキストビューに数字を表示するだけで、同じ機能を実現することはできませんか?編集テキストをクリアするのと同じことです。ここでのButterKnifeの使用法は過度の冗談だと私には思われます。そうでなければ整理できる機能のカスタムビューを作成しています。私が何かを見逃しているなら、遠くに行く理由を理解したいと思います。 – daxgirl

+0

私はを使ってレイアウト内に持っていましたが、問題はこの機能がすべて私のアプリケーション上にあったことです。私は1つのクリーンなカスタムビューにすることによってカウンタ/クリアを処理するためのすべての余分なコードを再現しようとしています。 –

答えて

1

私は後ででランタイムに記入し、XMLで空のレイアウトを使用するには、これらの状況では単純にそれを見つけます私が望む要素は何でも。例:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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"> 

    <TextView ..... /> 
    <Button........./> 
    . 
    . 
    . 
    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 


</LinearLayout> 

と活動の

MyComplexView

LayoutInflater inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
LinearLayout container = (LinearLayout)findViewById(R.id.container); 
MyComplexView myComplexView = new MyComplexView(inflater, container); 

public static class MyComplexView{ 

     LinearLayout container; 
     LayoutInflater inflater; 
     TextView textView ; 
     ImageView img; 
     EditText editText; 

     public MyComplexView(LinearLayout container,LayoutInflater inflater){ 
      this.container = container; 
      this.inflater = inflater; 
      View v = (View) inflater.inflate(R.layout.mylayout, null); 
      container.addView(v); 
      textView = (TextView)v.findViewById(R.id.textview); 
      img = (ImageView)v.findViewById(R.id.imgaviev); 
      editText = (EditText)v.findViewById(R.id.edittext); 
      // assign whatever text change listeners or on click listeners you want 
     } 

     public void makeEditTextMultiline(boolean flag){ 
      if(flag){  
       edittext.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE); 
      } 
      else{ 
       edittext.setSingleLine(true); 
      } 
     } 
     public String getEditTextText(){ 
      return editText.getText().toString(); 
     } 

    } 

その後はあなたがオブジェクトを操作するにはMyComplexViewクラスのメソッドのすべての種類を作成することができます。

私は、Viewクラスを拡張するよりも簡単な方法だと思います。

+0

それは興味深い解決策ですが、私が達成しようとしていることがうまくいくとは思いません。 EditTextを拡張して、code/xmlで直接編集できるようにしたい。たとえば、EditTextを1つのアクティビティで複数行にしたいが、別のアクティビティではないようにするには、拡張クラス内ではなく、複数のレイアウト、またはそのアクティビティ内の多くのコードが必要です。 –

+0

だからこそ、私はあなたがオブジェクトを操作するMyComplexViewクラス内のすべての種類のメソッドを追加することができると言いました。あなたは、EditTextのマルチラインを作るかどうかをsetEditTextMultine(boolean flag)のようなメソッドを追加するだけです。 – Anonymous

+0

EditTextを拡張して、説明することはできません。Viewクラスを拡張し、最初からカスタムビューを構築する必要があります。これはlooootよりも多くのコードです。答えを更新しました。今度は、オブジェクトを作成した後、複数行にするかどうかを指定できます。プログラム的にビューについて操作できないものはほとんどありません – Anonymous

関連する問題