Android 自定义输入框

Android 自定义输入框代码简单,输入框一般常用的控件,每次都写太麻烦,所以封装


 

 

package xyz.xl06.music.wigdet;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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 xyz.xl06.music.R;

public class InputView  extends FrameLayout {
    private int inputview_input_icon;
    private String inputview_input_hint;
    private boolean inputview_is_pass;
    private View inflate;
    ImageView imageView;
    EditText editText;
    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);
    }

    public void init(Context context,AttributeSet attr){
                 if(attr==null){
                     return;
                 }
        TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.inputview);
         inputview_input_icon = typedArray.getResourceId(R.styleable.inputview_input_icon, R.mipmap.ic_launcher);
         inputview_input_hint = typedArray.getString(R.styleable.inputview_input_hint);
         inputview_is_pass = typedArray.getBoolean(R.styleable.inputview_is_pass, false);
         typedArray.recycle();

         inflate = LayoutInflater.from(context).inflate(R.layout.inputview, this, false);
        imageView=  (ImageView)inflate.findViewById(R.id.icon);
        editText=  (EditText)inflate.findViewById(R.id.text);

        imageView.setImageResource(inputview_input_icon);
        editText.setText(inputview_input_hint);
        editText.setInputType(inputview_is_pass?InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD:InputType.TYPE_CLASS_PHONE);
        addView(inflate);
    }

    public String getString(){
        return  editText.getText().toString().trim();
    }
}

inputview.xml





    

使用

 
    

    
    

attr



    
        
        
        
        
        
        
    

 

你可能感兴趣的:(Android)