【问题标题】:Android - Custom View - Extending EditText with Views on TopAndroid - 自定义视图 - 使用顶部视图扩展 EditText
【发布时间】:2016-05-11 00:31:57
【问题描述】:

我正在尝试找出一个很好的解决方案来扩展 EditText 以允许我将其他视图分层。我正在尝试制作一个自定义视图,它是一个 EditText,顶部有一个 TextView 以显示字符数,顶部有一个 ImageView 用于清除按钮。

目前,我正在使用扩展 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 有点过头了。为可以以其他方式安排的功能创建自定义视图也是如此。如果我遗漏了什么,我想了解为什么要走这么长的路。
  • 我曾经使用 将它放在布局中,但问题是这个功能遍布我的应用程序。我正在尝试通过将其变为一个干净的自定义视图来重现用于处理计数器/清除的所有额外代码。

标签: java android android-edittext android-custom-view butterknife


【解决方案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 类更容易。

【讨论】:

  • 这是一个有趣的解决方案,但我认为它不适用于我想要完成的工作。我想扩展EditText,这样我就可以通过code/xml直接修改它。例如,如果我希望 EditText 在一个 Activity 中是多行的,而不是在另一个 Activity 中,我需要多个布局,或者在该 Activity 中使用您的方法编写大量代码,而不是在扩展类中。跨度>
  • 这就是为什么我说你可以在 MyComplexView 类中添加各种方法来操作对象。您只需添加一个类似 setEditTextMultine(boolean flag) 的方法,您可以在其中制作或不制作 EditText 多行
  • 你不能扩展 EditText 来做你所描述的事情。你必须扩展 View 类并从一开始就构建你的自定义视图。这是比这更多的代码。更新了答案。现在,在创建对象之后,您可以将其设为多行或不设为多行。以编程方式无法操作视图的事情非常少
猜你喜欢
  • 1970-01-01
  • 2014-03-05
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
  • 2016-09-21
  • 1970-01-01
相关资源
最近更新 更多