【问题标题】:How to make a reusable EditText with clear button?如何使用清除按钮制作可重复使用的 EditText?
【发布时间】:2012-04-20 00:55:55
【问题描述】:

我想制作一个带有清除按钮的 EditText 复合控件。由于主要功能将是 EditText 功能,我希望它可以在具有 EditText 的所有属性的布局 xml 中声明。但是,它实际上是一个带有 EditText 和 ImageButton 的 LinearLayout。

另外,我希望它像代码中的 EditText 一样使用,所以我可以将它作为替代品放入。

到目前为止我有这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:drawable/editbox_background" >
    <EditText 
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:background="#FFF" />
    <ImageButton
        android:id="@+id/clear"
        android:src="@drawable/clear_text"
        android:background="#FFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />
</LinearLayout>

但我不确定如何继续。如果我需要它是 LinearLayout 以使用此布局,如何定义扩展 EditText 的控件?还是我唯一的选择是在 onDraw() 中手动绘制 x 并自己处理点击?

【问题讨论】:

    标签: android android-layout android-widget android-ui android-custom-view


    【解决方案1】:
     public class myedittext extends LinearLayout{
     EditText e1;
      ImageButton ib;
      OnClickListener cleartext;
     public myedittext(Context c){
      super(c);
       String inflater=context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater lif;
      li=(LayoutInflater)getContext().getSystemService(inflater);
      li.inflate((R.layout.yourlayoutname,this,true);
     e1=(EditText)findViewById(R.id.editText);
       ib=(ImageButton)findViewById(R.id.clear);
    
    
      cleartext = new OnClickListener(){
    
        @Override
        public void onClick(View v) {
    
            e1.setText("");
        }
       };
      ib.setOnClickListener(cleartext);
     }
       }
    

    【讨论】:

      【解决方案2】:

      您可以像您的 XML 一样声明从 LinearLayout 扩展的自定义视图类。然后加载 Xml 并将其添加到您的自定义视图中。

      如果你想在 XML 中使用它,你应该首先通过 attrs.xml 声明你自己的 XML 属性,如下所示

      <declare-styleable name="CustomEditText">
          <attr name="text" format="string" />
      </declare-styleable>
      

      然后用 Attributes attrs 覆盖构造函数并解析 attrs 如下代码

      TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CustomEditText);
      CharSequence s = a.getString(R.styleable.CustomEditText_text);
      setText(s.toString());
      

      像这样在 XML 中使用它:

      xmlns:anything="http://schemas.android.com/apk/res/yourpackagename" // for declare your namespace
      <yourpackagename
              android:background="@drawable/blue"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" 
              anything:text="hi"/>
      

      要像代码中的editText 一样使用它,只需包装自定义视图类。

      【讨论】:

      • 太糟糕了,你需要用你自己的副本“替换”所有 EditText 特定属性,比如文本。这可能意味着工作量太大,具体取决于实际使用的属性数量。
      猜你喜欢
      • 2021-08-02
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 2014-04-14
      • 1970-01-01
      • 2014-07-04
      相关资源
      最近更新 更多