Andriod:自定义控件

我们经常写如下这样的登录注册页面

我们发现这些输入框都大同小异,无非就是两个子控件,三个属性

两个子控件:
  1. ImageView: iv_input_icon
  2. EditText: et_input_text
三个属性:
  1. inputIcon: 图标
  2. inputHint: 提示
  3. isPassword: 文本是否以密文的形式

所以我们可以把这些InputText封装成自己的控件

1.在values文件夹下新建attrs.xml

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="InputView">
        <attr name="input_icon" format="reference"></attr>
        <attr name="input_hint" format="string"></attr>
        <attr name="is_password" format="boolean"></attr>
    </declare-styleable>
</resources>
2.在layout文件夹下新建input_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    >
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_marginTop="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/phone" />
    <!--background="@null" 去除下划线-->
    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:hint="用户名"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textSize="18sp" />
</LinearLayout>
3.在java文件夹下新建类InputView继承FrameLayout

InputView.class

public class InputView extends FrameLayout {
    //三个属性
    private int inputIcon;
    private String inputHint;
    private Boolean isPassword;
    private View view;
    //两个控件
    private ImageView iv_input_icon;
    private EditText et_input_text;

    public InputView(Context context) {
        super(context);
    }

    public InputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initInputView(context,attrs);
    }

    public InputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initInputView(context,attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public InputView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initInputView(context,attrs);

    }

    private void initInputView(Context context, AttributeSet attrs) {
        if (attrs == null) return;

        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.InputView);
        inputIcon = typedArray.getResourceId(R.styleable.InputView_input_icon, R.mipmap.logo);
        inputHint = typedArray.getString(R.styleable.InputView_input_hint);
        isPassword = typedArray.getBoolean(R.styleable.InputView_is_password, false);
        //记得调用recycle方法
        typedArray.recycle();

        //绑定layout布局
        view = LayoutInflater.from(context).inflate(R.layout.input_view, this, false);
        iv_input_icon = view.findViewById(R.id.iv_icon);
        et_input_text= view.findViewById(R.id.et_input);

        //布局关联属性
        iv_input_icon.setImageResource(inputIcon);
        et_input_text.setHint(inputHint);
        et_input_text.setInputType(isPassword ? InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_PHONE);
        addView(view);
    }

    /**
     * 返回输入的内容
     * @return
     */

    public String getInputStr(){
        return et_input_text.getText().toString().trim();
    }
}
4.这样我们就将自己的控件建立好了,然后在activity_login中使用自己的控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LoginActivity">
    <!--通过include标签引入其他layout-->
    <include layout="@layout/nav_bar" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/marginSize"
        android:src="@mipmap/logo" />

    <com.wantao.music.Views.InputView
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="16dp"
        app:input_hint="请输入手机号"
        app:input_icon="@mipmap/phone"
        app:is_password="false"></com.wantao.music.Views.InputView>

    <com.wantao.music.Views.InputView
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="16dp"
        app:input_hint="请输入密码"
        app:input_icon="@mipmap/password"
        app:is_password="true">
    </com.wantao.music.Views.InputView>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Selenium399

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值