如何填充自定义EditText字符的底部?

时间:2016-03-05 13:10:15

标签: android android-edittext

我有一个自定义的EditText字符,带有一些简单的字符和图像。

如下图所示:enter image description here

但我希望图像填充底部像这样的图像:

enter image description here

这是我的pwd_bullet.png:enter image description here

这是我的代码:

  1. custom_edittext.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5b5555"
    tools:context="com.example.user.myapplication.Custom_Edittext">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:id="@+id/editText"
                android:inputType="text"
                android:gravity="center_vertical"
                android:background="@null"/>
        </LinearLayout>
    </RelativeLayout>
    
  2. Custom_Edittext.java

    public class Custom_Edittext extends Activity implements TextWatcher{
         EditText editText;
         Spannable.Factory spannableFactory;
         int lastIndex = -1;
    
    protected void onCreate(Bundle savedInstanceState) {
         enter code here`super.onCreate(savedInstanceState);
         setContentView(R.layout.custom_edittext);
         editText = (EditText) findViewById(R.id.editText);
         spannableFactory = Spannable.Factory
            .getInstance();
         editText.addTextChangedListener(this);
    }
    public Spannable getIconText(Context context, CharSequence text, int index) {
        Spannable spannable = spannableFactory.newSpannable(text);
        if (index>lastIndex) {
            spannable.setSpan(new ImageSpan(context, R.drawable.pwd_bullet),
                index, index + 1,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        lastIndex=index;
        return spannable;
    }
    
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (start>4) {
            editText.removeTextChangedListener(this);
            editText.setText(getIconText(getApplicationContext(), s, start));
            editText.addTextChangedListener(this);
            editText.setSelection(s.length());
        }
    }
    
    public void afterTextChanged(Editable s) {}
    }
    

2 个答案:

答案 0 :(得分:1)

在这种情况下不需要使用填充,只需使用线性布局,因为您的项目是直线,并确保将线性布局设置为方向和重力,如下所示。这对我有用。祝你好运

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:id="@+id/editText"
        android:inputType="text"
        android:gravity="center_vertical"
        android:background="@null"
        android:text="Simple Text" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/pwd_bullet.png"/>
</LinearLayout>

答案 1 :(得分:1)

在drawable中创建一个自定义pwd_bullet的xml

list