• 自定义LinearLayout 实现不同输入框效果


    1、实现左侧、右侧、中间三个部分可以默认设置字体, 中间是输入框

    package com.hospital.widget;

    import org.apache.commons.lang3.StringUtils;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.drawable.Drawable;
    import android.text.Editable;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import com.hospital.R;

    public class CommonItemValueLable extends LinearLayout {

    private LayoutInflater mInflater;
    private View mItem;
    private TextView tv_title;
    private TextView tv_union;
    private TextView tv_one;
    private LinearLayout ll_right;
    private boolean item_isEdit;
    private String item_title_text;
    private String item_context_text;
    private String itemvalue_context_textvalueunion;
    private String itemvalue_context_textvaluehint;
    private String itemvalue_context_textvalueinputtype;
    private OnClickListener mOnClickListener;
    private FocuslostListener mFocuslostListener;

    public interface FocuslostListener {
    void onFocusLost(View v);
    }

    public interface OnClickListener {
    void onClick(View v);
    }

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

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

    public void setOnClickListener(OnClickListener l) {
    this.mOnClickListener = l;
    }

    public void setFocuslostListener(FocuslostListener l) {
    this.mFocuslostListener = l;
    }

    public CommonItemValueLable(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mInflater = LayoutInflater.from(context);
    mItem = mInflater.inflate(R.layout.common_item_value_lable, null);
    LinearLayout.LayoutParams mParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    mItem.setLayoutParams(mParams);
    addView(mItem);
    TypedArray item = context.obtainStyledAttributes(attrs, R.styleable.itemvalue);
    item_isEdit = item.getBoolean(R.styleable.itemvalue_isEditvalue, true);
    item_title_text = item.getString(R.styleable.itemvalue_title_textvalue);
    item_context_text = item.getString(R.styleable.itemvalue_context_textvalue);
    itemvalue_context_textvalueunion = item.getString(R.styleable.itemvalue_context_textvalueunion);
    itemvalue_context_textvaluehint = item.getString(R.styleable.itemvalue_context_textvaluehint);
    itemvalue_context_textvalueinputtype = item.getString(R.styleable.itemvalue_context_textvalueinputtype);
    item.recycle();
    initViews(context);
    }

    private void initViews(Context context) {
    tv_title = (TextView) mItem.findViewById(R.id.tv_title);
    tv_one = (TextView) mItem.findViewById(R.id.tv_one);
    ll_right = (LinearLayout) mItem.findViewById(R.id.ll_right);
    if(StringUtils.isNotEmpty(itemvalue_context_textvaluehint))
    tv_one.setHint(itemvalue_context_textvaluehint);
    tv_union = (TextView) mItem.findViewById(R.id.tv_union);

    tv_union.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.dimens_6_160));
    Drawable right = getResources().getDrawable(R.drawable.qw);
    right.setBounds(0, 0, right.getMinimumWidth(), right.getMinimumHeight());
    tv_union.setCompoundDrawables(null, null, right, null);

    tv_title.setText(item_title_text);
    tv_one.setText(item_context_text);
    if(itemvalue_context_textvalueinputtype!=null&&"number".equals(itemvalue_context_textvalueinputtype))
    tv_one.setInputType(0x00000002);
    tv_union.setText(itemvalue_context_textvalueunion);
    if (item_isEdit) {
    tv_one.setFocusable(true);
    tv_one.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus) {
    if (mFocuslostListener != null) {
    mFocuslostListener.onFocusLost(v);
    }
    }
    }
    });
    } else {
    tv_one.setFocusable(false);
    tv_one.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    if (mOnClickListener != null) {
    mOnClickListener.onClick(v);
    }
    }
    });
    }
    }

    public void setContentValue(String contentValue) {
    tv_one.setText(contentValue);
    }

    public CharSequence getContentValue() {
    return tv_one.getText();
    }

    public TextView getTextView() {
    return tv_one;
    }

    public LinearLayout getLlRight(){
    return ll_right;
    }

    public void setContentValueUnion(String contentValue) {
    tv_union.setText(contentValue);
    }

    public CharSequence getContentValueUnion() {
    return tv_union.getText();
    }

    }

    <?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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
    android:id="@+id/tv_title"
    style="@style/text_show_black_36_p1"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:text="药物过敏史" />

    <LinearLayout
    android:id="@+id/ll_right"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="right"
    android:orientation="horizontal" >

    <TextView
    android:id="@+id/tv_one"
    style="@style/text_show_black_36_p1"
    android:background="@null"
    android:gravity="right"
    android:maxLines="1"
    android:text="男" />

    <TextView
    android:id="@+id/tv_union"
    android:layout_marginLeft="@dimen/dimens_6_160"
    style="@style/text_show_black_36_p1"
    android:text="元" />
    </LinearLayout>

    </LinearLayout>

    2、实现左侧、右侧两个部分可以默认设置字体, 中间是输入框

    package com.hospital.widget;

    import com.hospital.R;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.drawable.Drawable;
    import android.text.Editable;
    import android.text.InputFilter;
    import android.text.InputType;
    import android.text.method.ScrollingMovementMethod;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnFocusChangeListener;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.LinearLayout.LayoutParams;

    public class CommonItem extends LinearLayout {

    private LayoutInflater mInflater;
    private View mItem;
    private TextView tv_title;
    private EditText et_one;
    private boolean item_isEdit;
    private boolean item_isrighticon;
    private String item_title_text;
    private int context_textinputtype;
    private String item_context_text;
    private String item_context_textinputtype;
    private OnClickListener mOnClickListener;
    private FocuslostListener mFocuslostListener;

    public interface FocuslostListener {
    void onFocusLost(View v);
    }

    public interface OnClickListener {
    void onClick(View v);
    }

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

    public CommonItem(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    }
    //点击
    public void setOnClickListener(OnClickListener l) {
    this.mOnClickListener = l;
    }
    //失去焦点
    public void setFocuslostListener(FocuslostListener l) {
    this.mFocuslostListener = l;
    }

    public CommonItem(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mInflater = LayoutInflater.from(context);
    mItem = mInflater.inflate(R.layout.common_item, null);
    LinearLayout.LayoutParams mParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    mItem.setLayoutParams(mParams);
    addView(mItem);
    TypedArray item = context.obtainStyledAttributes(attrs, R.styleable.item);
    item_isEdit = item.getBoolean(R.styleable.item_isEdit, true);//是否可以编辑
    item_isrighticon = item.getBoolean(R.styleable.item_isrighticon, true);//是否右面箭头
    item_title_text = item.getString(R.styleable.item_title_text);//左面的字
    item_context_text = item.getString(R.styleable.item_context_text);//右面的字

    item_context_textinputtype = item.getString(R.styleable.item_context_textinputtype);

    item.recycle();
    initViews(context);
    }

    private void initViews(Context context) {
    tv_title = (TextView) mItem.findViewById(R.id.tv_title);
    et_one = (EditText) mItem.findViewById(R.id.et_one);
    et_one.setHorizontallyScrolling(false);
    et_one.setFilters(new InputFilter[]{new InputFilter.LengthFilter(200)}); // 添加输入长度限制
    et_one.setMovementMethod(ScrollingMovementMethod.getInstance());
    tv_title.setText(item_title_text);
    et_one.setText(item_context_text);
    if(item_context_textinputtype!=null&&item_context_textinputtype.equals("number"))
    et_one.setRawInputType(InputType.TYPE_CLASS_NUMBER);
    if (item_isrighticon) {
    et_one.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.dimens_6_160));
    Drawable drawable = getResources().getDrawable(R.drawable.qw);
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    et_one.setCompoundDrawables(null, null, drawable, null);
    }

    if (item_isEdit) {
    et_one.setFocusable(true);
    et_one.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus) {
    if (mFocuslostListener != null) {
    mFocuslostListener.onFocusLost(v);
    }
    }
    }
    });
    } else {
    et_one.setFocusable(false);
    et_one.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    if (mOnClickListener != null) {
    mOnClickListener.onClick(v);
    }
    }
    });
    tv_title.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    if (mOnClickListener != null) {
    mOnClickListener.onClick(v);
    }
    }
    });
    }
    }

    public void setContentValue(String contentValue) {
    et_one.setText(contentValue);
    }

    public Editable getContentValue() {
    return et_one.getText();
    }
    }

    <?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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
    android:id="@+id/tv_title"
    style="@style/text_show_black_36_p1"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:text="药物过敏史" />

    <EditText
    android:id="@+id/et_one"
    style="@style/text_show_black_36_p1"
    android:layout_width="0dp"
    android:inputType="textMultiLine"
    android:layout_weight="1"
    android:background="@null"
    android:gravity="right"
    android:maxLines="2"
    android:text="男" />

    </LinearLayout>

    3、实现左侧、中间、右侧三部分可以默认设置字体, 中间是输入框和1样式不同

    package com.hospital.widget;

    import org.apache.commons.lang3.StringUtils;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.text.Editable;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import com.hospital.R;

    public class CommonItemValue extends LinearLayout {

    private LayoutInflater mInflater;
    private View mItem;
    private TextView tv_title;
    private TextView tv_union;
    private EditText et_one;
    private boolean item_isEdit;
    private boolean isimportant;
    private String item_title_text;
    private String item_context_text;
    private String itemvalue_context_textvalueunion;
    private String itemvalue_context_textvaluehint;
    private String itemvalue_context_textvalueinputtype;
    private OnClickListener mOnClickListener;
    private FocuslostListener mFocuslostListener;

    public interface FocuslostListener {
    void onFocusLost(View v);
    }

    public interface OnClickListener {
    void onClick(View v);
    }

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

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

    public void setOnClickListener(OnClickListener l) {
    this.mOnClickListener = l;
    }

    public void setFocuslostListener(FocuslostListener l) {
    this.mFocuslostListener = l;
    }

    public CommonItemValue(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mInflater = LayoutInflater.from(context);
    mItem = mInflater.inflate(R.layout.common_item_value, null);
    LinearLayout.LayoutParams mParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    mItem.setLayoutParams(mParams);
    addView(mItem);
    TypedArray item = context.obtainStyledAttributes(attrs, R.styleable.itemvalue);
    item_isEdit = item.getBoolean(R.styleable.itemvalue_isEditvalue, true);
    isimportant = item.getBoolean(R.styleable.itemvalue_isimportant, true);
    item_title_text = item.getString(R.styleable.itemvalue_title_textvalue);
    item_context_text = item.getString(R.styleable.itemvalue_context_textvalue);
    itemvalue_context_textvalueunion = item.getString(R.styleable.itemvalue_context_textvalueunion);
    itemvalue_context_textvaluehint = item.getString(R.styleable.itemvalue_context_textvaluehint);
    itemvalue_context_textvalueinputtype = item.getString(R.styleable.itemvalue_context_textvalueinputtype);
    item.recycle();
    initViews(context);
    }

    private void initViews(Context context) {
    tv_title = (TextView) mItem.findViewById(R.id.tv_title);
    et_one = (EditText) mItem.findViewById(R.id.et_one);
    if(StringUtils.isNotEmpty(itemvalue_context_textvaluehint))
    et_one.setHint(itemvalue_context_textvaluehint);
    tv_union = (TextView) mItem.findViewById(R.id.tv_union);
    tv_title.setText(item_title_text);
    et_one.setText(item_context_text);
    if(isimportant){
    et_one.setTextColor(getContext().getResources().getColor(R.color.background_patient_money));
    }
    if(itemvalue_context_textvalueinputtype!=null&&"number".equals(itemvalue_context_textvalueinputtype))
    et_one.setInputType(0x00000002);
    tv_union.setText(itemvalue_context_textvalueunion);
    if (item_isEdit) {
    et_one.setFocusable(true);
    et_one.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus) {
    if (mFocuslostListener != null) {
    mFocuslostListener.onFocusLost(v);
    }
    }
    }
    });
    } else {
    et_one.setFocusable(false);
    et_one.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    if (mOnClickListener != null) {
    mOnClickListener.onClick(v);
    }
    }
    });
    }
    }

    public void setContentValue(String contentValue) {
    et_one.setText(contentValue);
    }

    public Editable getContentValue() {
    return et_one.getText();
    }

    public void setContentValueUnion(String contentValue) {
    tv_union.setText(contentValue);
    }

    public CharSequence getContentValueUnion(String contentValue) {
    return tv_union.getText();
    }

    }

    package com.hospital.widget;

    import org.apache.commons.lang3.StringUtils;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.text.Editable;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import com.hospital.R;

    public class CommonItemValue extends LinearLayout {

    private LayoutInflater mInflater;
    private View mItem;
    private TextView tv_title;
    private TextView tv_union;
    private EditText et_one;
    private boolean item_isEdit;
    private boolean isimportant;
    private String item_title_text;
    private String item_context_text;
    private String itemvalue_context_textvalueunion;
    private String itemvalue_context_textvaluehint;
    private String itemvalue_context_textvalueinputtype;
    private OnClickListener mOnClickListener;
    private FocuslostListener mFocuslostListener;

    public interface FocuslostListener {
    void onFocusLost(View v);
    }

    public interface OnClickListener {
    void onClick(View v);
    }

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

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

    public void setOnClickListener(OnClickListener l) {
    this.mOnClickListener = l;
    }

    public void setFocuslostListener(FocuslostListener l) {
    this.mFocuslostListener = l;
    }

    public CommonItemValue(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mInflater = LayoutInflater.from(context);
    mItem = mInflater.inflate(R.layout.common_item_value, null);
    LinearLayout.LayoutParams mParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    mItem.setLayoutParams(mParams);
    addView(mItem);
    TypedArray item = context.obtainStyledAttributes(attrs, R.styleable.itemvalue);
    item_isEdit = item.getBoolean(R.styleable.itemvalue_isEditvalue, true);
    isimportant = item.getBoolean(R.styleable.itemvalue_isimportant, true);
    item_title_text = item.getString(R.styleable.itemvalue_title_textvalue);
    item_context_text = item.getString(R.styleable.itemvalue_context_textvalue);
    itemvalue_context_textvalueunion = item.getString(R.styleable.itemvalue_context_textvalueunion);
    itemvalue_context_textvaluehint = item.getString(R.styleable.itemvalue_context_textvaluehint);
    itemvalue_context_textvalueinputtype = item.getString(R.styleable.itemvalue_context_textvalueinputtype);
    item.recycle();
    initViews(context);
    }

    private void initViews(Context context) {
    tv_title = (TextView) mItem.findViewById(R.id.tv_title);
    et_one = (EditText) mItem.findViewById(R.id.et_one);
    if(StringUtils.isNotEmpty(itemvalue_context_textvaluehint))
    et_one.setHint(itemvalue_context_textvaluehint);
    tv_union = (TextView) mItem.findViewById(R.id.tv_union);
    tv_title.setText(item_title_text);
    et_one.setText(item_context_text);
    if(isimportant){
    et_one.setTextColor(getContext().getResources().getColor(R.color.background_patient_money));
    }
    if(itemvalue_context_textvalueinputtype!=null&&"number".equals(itemvalue_context_textvalueinputtype))
    et_one.setInputType(0x00000002);
    tv_union.setText(itemvalue_context_textvalueunion);
    if (item_isEdit) {
    et_one.setFocusable(true);
    et_one.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus) {
    if (mFocuslostListener != null) {
    mFocuslostListener.onFocusLost(v);
    }
    }
    }
    });
    } else {
    et_one.setFocusable(false);
    et_one.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    if (mOnClickListener != null) {
    mOnClickListener.onClick(v);
    }
    }
    });
    }
    }

    public void setContentValue(String contentValue) {
    et_one.setText(contentValue);
    }

    public Editable getContentValue() {
    return et_one.getText();
    }

    public void setContentValueUnion(String contentValue) {
    tv_union.setText(contentValue);
    }

    public CharSequence getContentValueUnion(String contentValue) {
    return tv_union.getText();
    }

    }

  • 相关阅读:
    Swift_Set详解
    Swift_数组详解
    Swift_字符串详解(String)
    选择排序_C语言_数组
    插入排序_C语言_数组
    快速排序_C语言_数组
    冒泡排序_C语言_数组
    七牛直播收获分享
    iOS 实用博客整理(连载版)
    iOS Swift-元组tuples(The Swift Programming Language)
  • 原文地址:https://www.cnblogs.com/achen0502/p/5466311.html
Copyright © 2020-2023  润新知