Android自定義view實(shí)現(xiàn)輸入框效果
本文實(shí)例為大家分享了Android自定義view實(shí)現(xiàn)輸入框的具體代碼,供大家參考,具體內(nèi)容如下
自定義輸入框的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);
//設(shè)置樣式,是不是密文格式,可以沒(méi)有.
editText.setInputType(inputview_is_pass? InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD:InputType.TYPE_CLASS_PHONE);
//添加到viewgroup中
addView(inflate);
}
//用來(lái)取到輸入框的值.
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="手機(jī)號(hào)"
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>
效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼
這篇文章主要介紹了Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android事件分發(fā)機(jī)制?ViewGroup分析
這篇文章主要介紹了Android事件分發(fā)機(jī)制?ViewGroup分析,事件分發(fā)從手指觸摸屏幕開始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會(huì)傳遞給Android的輸入系統(tǒng)服務(wù)IMS,更多相關(guān)介紹,需要的朋友可以參考一下2022-09-09
Android中應(yīng)用界面主題Theme使用方法和頁(yè)面定時(shí)跳轉(zhuǎn)應(yīng)用
在Android SDK中內(nèi)置了下面的Theme,可以按標(biāo)題欄Title Bar和狀態(tài)欄Status Bar是否可見(jiàn)來(lái)分類,感興趣的朋友可以了解下哈2013-06-06
Android中Market的Loading效果實(shí)現(xiàn)方法
這篇文章主要介紹了Android中Market的Loading效果實(shí)現(xiàn)方法,較為詳細(xì)的分析了Android中l(wèi)oading效果的相關(guān)布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android實(shí)用的代碼片段 常用代碼總結(jié)
這篇文章主要介紹了Android實(shí)用的代碼片段 常用代碼總結(jié),需要的朋友可以參考下2014-09-09
android側(cè)滑菜單控件DrawerLayout使用方法詳解
這篇文章主要為大家詳細(xì)介紹了android側(cè)滑菜單控件DrawerLayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android開發(fā)實(shí)現(xiàn)帶有反彈效果仿IOS反彈scrollview教程詳解
本文給大家分享android開發(fā)實(shí)現(xiàn)帶有反彈效果,模仿ios反彈scrollview詳細(xì)教程,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09
Android實(shí)現(xiàn)點(diǎn)擊兩次返回鍵退出
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊兩次返回鍵退出的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

