Android自定义view实现输入框效果

 更新时间:2021年03月15日 08:15:31   作者:天真的赵日天  
这篇文章主要为大家详细介绍了Android自定义view实现输入框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android自定义view实现输入框的具体代码,供大家参考,具体内容如下

自定义输入框的View

package com.fenghongzhang.day017;

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

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

public class InputView extends LinearLayout {
  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);
  }

  public InputView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    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);
    
    //添加到viewgroup中
    addView(inflate);
  }

  //用来取到输入框的值.
  public String getString(){
    return editText.getText().toString().trim();
  }

}

输入框的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="300dp"
  android:orientation="horizontal"
  android:layout_height="50dp"
  android:background="@drawable/back_color"
  android:layout_gravity="center_vertical"
  >
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    android:layout_gravity="center_vertical"
    android:id="@+id/icon"
    />

  <EditText
    android:layout_marginLeft="30dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:hint="username"
    android:textSize="30dp"
    android:background="@null"
    android:id="@+id/text"
    />
</LinearLayout>

属性文件

<?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_pass" format="boolean"></attr>
  </declare-styleable>
  
</resources>

布局中引用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:my="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".Main3Activity">
  
  <com.fenghongzhang.day017.InputView
    android:layout_width="300dp"
    android:layout_height="50dp"
    my:input_icon="@mipmap/ic_launcher"
    my:input_hint="手机号"
    my:is_pass="true"
    >
  </com.fenghongzhang.day017.InputView>

</LinearLayout>

输入框圆角背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  
  <corners android:radius="10dp" ></corners>
  <size android:width="100dp" android:height="30dp"></size>
  <solid android:color="@color/colorAccent"></solid>
</shape>

效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android无需权限调起系统相机

    Android无需权限调起系统相机

    在进行一些小型APP的开发,或者是对拍照界面没有自定义要求时,我们可以用调起系统相机的方式快速完成拍照需求
    2023-03-03
  • Android编程之自定义AlertDialog(退出提示框)用法实例

    Android编程之自定义AlertDialog(退出提示框)用法实例

    这篇文章主要介绍了Android编程之自定义AlertDialog(退出提示框)用法,结合实例形式较为详细的分析了自定义AlertDialog的页面布局与功能实现相关技巧,需要的朋友可以参考下
    2016-01-01
  • Android源码学习之组合模式定义及应用

    Android源码学习之组合模式定义及应用

    将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性,需要了解的朋友可以参考下
    2013-01-01
  • Android实现带描边的圆角图片

    Android实现带描边的圆角图片

    这篇文章主要为大家详细介绍了Android实现带描边的圆角图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • AndroidStudio实现能在图片上涂鸦程序

    AndroidStudio实现能在图片上涂鸦程序

    这篇文章主要为大家详细介绍了AndroidStudio实现能在图片上涂鸦程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Android Fragment使用全解

    Android Fragment使用全解

    这篇文章主要介绍了Android Fragment使用的相关资料,帮助大家更好的理解和学习使用Android开发,感兴趣的朋友可以了解下
    2021-04-04
  • Android微信签名知识的总结

    Android微信签名知识的总结

    这篇文章给大家详细总结了Android微信签名用到的知识,文中通过具体的实现过程给大家进行演示,相信对大家的理解很有帮助,下面来一起看看吧。
    2016-09-09
  • Android解决dialog弹出时无法捕捉Activity的back事件的方法

    Android解决dialog弹出时无法捕捉Activity的back事件的方法

    这篇文章主要介绍了Android解决dialog弹出时无法捕捉Activity的back事件的方法,涉及Android操作Activity事件的相关技巧,需要的朋友可以参考下
    2015-05-05
  • android监听器实例代码

    android监听器实例代码

    在本篇中小编给大家整理了一篇关于android监听器的相关知识点文章,需要的朋友们可以学习下。
    2019-10-10
  • Android 消息分发使用EventBus的实例详解

    Android 消息分发使用EventBus的实例详解

    这篇文章主要介绍了Android 消息分发使用EventBus的实例详解的相关资料,在项目中用了许多Handler和broadcast导致代码冗余,显得杂乱无章,这里使用EventBus来实现相同的功能,需要的朋友可以参考下
    2017-07-07

最新评论