左侧文字输入框

效果如下


image.png

主要框架代码如下:
TextInputBox.java

package com.qiyuan.gamesdk.core.ui.dialog.biz.View;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qiyuan.gamesdk.BuildConfig;
import com.qiyuan.gamesdk.R;
import com.qiyuan.gamesdk.core.CoreManager;

public class TextInputBox extends RelativeLayout {

    private TextView tv_left_text;
    private EditText edt_input;

    public TextInputBox(Context context) {
        this(context, null);
    }

    public TextInputBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextInputBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (BuildConfig.isApp) {
            LayoutInflater.from(CoreManager.getContext()).inflate(R.layout.text_input_box, this);
        } else {
            LayoutInflater.from(context).inflate(R.layout.text_input_box, this);
        }
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        edt_input = (EditText) findViewById(R.id.edt_input);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextInputBox, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.TextInputBox_leftText:
                    tv_left_text.setText(array.getString(attr));
                    break;
                case R.styleable.TextInputBox_leftTextColor:
                    tv_left_text.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.TextInputBox_leftTextSize:
                    tv_left_text.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;

                case R.styleable.TextInputBox_rightText:
                    edt_input.setText(array.getString(attr));
                    break;
                case R.styleable.TextInputBox_rightHint:
                    edt_input.setHint(array.getString(attr));
                    break;
                case R.styleable.TextInputBox_rightTextColor:
                    edt_input.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.TextInputBox_rightTextSize:
                    edt_input.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;
            }
        }
        array.recycle();
    }

}

布局文件text_input_box.xml




    

    

在values文件夹下,还有属性文件text_input_box_attrs.xml



    
    
        
        
        

        
        
        
        
    

在drawable文件夹下,还有个背景文件qy_sdk_edt_bg.xml




      //0.5dp
    
      //#00000000


至此主要的代码框架就完成了,代码功能可以继续完善,例如对于里面当个的控件添加单独的样式控制、点击事件等等。

你可能感兴趣的:(左侧文字输入框)