安卓开发学习——day11

写在前面

在写程序的过程中,我们会遇到重复写很多代码的问题,今天我们使用自定义控件进一步步提升效率。
,
正常我们写这么一个页面的输入框那些,每个都需要下面这些代码。

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">
        <ImageView
            android:id="@+id/iv_icon"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@mipmap/phone"/>

        <EditText
            android:id="@+id/et_input"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@null"
            android:hint="用户名"
            android:paddingLeft="@dimen/marginSize"
            android:paddingRight="@dimen/marginSize"
            android:textSize="@dimen/titleSize"/>
    </LinearLayout>

自定义控件

设置

[value/arrts.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>

[value/input_view]

<?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="@dimen/inputViewHeight"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/marginSize"
    android:paddingRight="@dimen/marginSize"
    >

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@mipmap/phone"/>

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:hint="用户名"
        android:paddingLeft="@dimen/marginSize"
        android:paddingRight="@dimen/marginSize"
        android:textSize="@dimen/titleSize"/>
</LinearLayout>

[java/views/inputView.java]
继承FrameLayout->重写方法,然后在下面定义初始化这些方法

package net.tyao.imoocmusicdemo.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import net.tyao.imoocmusicdemo.R;

/***
 * 1、input_icon 输入框前面的小图标
 * 2、input_hint 输入框的提示内容
 * 3、is_password 输入的内容是否需要以密文形式显示
 */
public class inputView extends FrameLayout {
    
    

    private int inputIcon;
    private String inputHint;
    private boolean isPassword;

    private View mView;
    private ImageView mIvIcon;
    private EditText mEtInput;

    public inputView(@NonNull Context context) {
    
    
        super(context);
        init(context, null);
    }

    public inputView(@NonNull Context context, @Nullable AttributeSet attrs) {
    
    
        super(context, attrs);
        init(context, attrs);
    }

    public inputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    
    
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) //android 5.0以上才可以使用
    public inputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    
    
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(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);
        typedArray.recycle(); //使用完成后记得释放

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

        // 布局关联属性
        mIvIcon.setImageResource(inputIcon);
        mEtInput.setHint(inputHint);
        mEtInput.setInputType(isPassword ? InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_NUMBER);
        addView(mView);
    }

    /***
     * 获取输入内容
     * @return
     */
    public String getInputStr() {
    
    
        return mEtInput.getText().toString().trim();
    }
}

使用

    <net.tyao.imoocmusicdemo.views.inputView
        android:layout_width="match_parent"
        android:layout_height="@dimen/inputViewHeight"
        android:layout_marginTop="@dimen/marginSize"
        app:input_icon="@mipmap/phone"
        app:input_hint="请输入手机号"
        ></net.tyao.imoocmusicdemo.views.inputView>

总结

可能这看起来写的比较多,但是到了后面可以直接使用,如果遇到很多这种,写起来就方便很多。

猜你喜欢

转载自blog.csdn.net/weixin_45936162/article/details/113092199