0

我想找出一个很好的解决方案,如何扩展EditText,让我有顶层的其他视图。我正在试图制作一个自定义的View,它是一个EditText,它具有顶部的TextView以显示字符数量,顶部的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

我发现它在这些情况下,简单的使用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> 

,并在活动

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

其中MyComplexView:

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,所以我可以通过代码/ xml直接修改它。例如,如果我希望EditText在一个Activity中是多行的,但不在另一个Activity中,我需要多个布局或该Activity中的许多代码与您的方法,而不是在扩展类中。 –

+0

这就是为什么我说你可以在MyComplexView类中添加各种方法来操纵对象。您只需添加一个像setEditTextMultine(布尔标志)的方法,您可以在其中制作EditText多行文件 – Anonymous

+0

您不能扩展EditText以执行您所描述的内容。您必须扩展View类并从开始构建自定义视图。这是比这更多的代码。更新了答案。在创建对象之后,您可以将其设为多行或不行。关于视图programmaticaly,您无法操作的东西极其稀少 – Anonymous