(安卓)自定义控件,自定义(view)中的自定义属性
来源:互联网 发布:audition mac 破解版 编辑:程序博客网 时间:2024/05/18 00:18
先来看看主xml中的布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="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="bwie.com.day_custom_control_1213.MainActivity"> <bwie.com.day_custom_control_1213.AddDelView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ade" app:left_text="加" app:right_text="减" app:middle_text="5" app:left_text_color="#03fbde" ></bwie.com.day_custom_control_1213.AddDelView></RelativeLayout>
在上面的 布局中可以看到: 整体是一个自定义控件
这些是自定义属性
首先自定义属性需要在values文件下写一个attrs.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="AddDelViewStyle"> <attr name="left_text" format="string"/> <attr name="right_text" format="string"/> <attr name="middle_text" format="string"/> <attr name="left_text_color" format="color"/> </declare-styleable></resources>
这是自定义属性所需要的
接下来看看自定义控件的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:padding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add" android:text="+" android:background="#aea9a9" /> <EditText android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/et" android:text="1" android:gravity="center" /> <TextView android:padding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/del" android:text="-" android:background="#aea9a9" /></LinearLayout>
=======================================================
上代码:
首先看看主MainActivity中的代码:
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity { private AddDelView ade; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //在主xml中引入自己写的自定义布局的文件 //自定义的布局控件 ade = (AddDelView)findViewById(R.id.ade); //自定义控件的加减以及EditText 。这三个的回调接口 ade.setOnAddDelClickLinstener(new AddDelView.OnAddDelClickLinstener() { @Override public void onAddClick(View v) { int number = ade.getNumber(); number++; ade.setNumber(number); } @Override public void onDelClick(View v) { int number = ade.getNumber(); number--; ade.setNumber(number); } }); }}
接下来自定义控件中的代码
AddDelView
import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;/** * Created by 迷人的脚毛!! on 2017/12/13. */public class AddDelView extends LinearLayout { private EditText et; //定义接口 interface OnAddDelClickLinstener{ void onAddClick(View v); void onDelClick(View v); } //定义接口方法 private OnAddDelClickLinstener linsanter; //定义接口方法 public void setOnAddDelClickLinstener(OnAddDelClickLinstener linsanter){ this.linsanter=linsanter; } public AddDelView(Context context) { this(context,null); } public AddDelView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public AddDelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs, defStyleAttr); } private void initView(Context context, AttributeSet attrs, int defStyleAttr) { View view = View.inflate(context, R.layout.layout_del_add, this); TextView add=(TextView) findViewById(R.id.add); TextView del=(TextView) findViewById(R.id.del); et = (EditText)findViewById(R.id.et); //自定义属性 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddDelViewStyle); String leftText = typedArray.getString(R.styleable.AddDelViewStyle_left_text); String rightText = typedArray.getString(R.styleable.AddDelViewStyle_right_text); String middleText = typedArray.getString(R.styleable.AddDelViewStyle_middle_text); int color = typedArray.getColor(R.styleable.AddDelViewStyle_left_text_color, Color.RED); //自定义属性设置 add.setText(leftText); add.setTextColor(color); del.setText(rightText); et.setText(middleText); //因为是数组,所以需要回收 typedArray.recycle(); //设置接口点击事件 //加 add.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { linsanter.onAddClick(view); } }); //减 del.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { linsanter.onDelClick(view); } }); } //定义赋值的方法 public void setNumber(int num){ if (num>0){ et.setText(num+""); } } //定义取值的方法 public int getNumber(){ int aa=0; try { String trim = et.getText().toString().trim(); aa = Integer.valueOf(trim + ""); }catch (Exception o){ aa=0; } return aa; }}
以上就是最基础的自定义控件以及自定义属性。。。。。
阅读全文
0 0
- (安卓)自定义控件,自定义(view)中的自定义属性
- 安卓自定义控件(三)实现自定义View
- 安卓自定义控件,自定义控件属性
- 自定义view(自定义属性)
- 【安卓笔记】带自定义属性的view控件
- #安卓杂记(七):自定义控件及属性获取
- 安卓开发-自定义view(二)-自定义属性
- 【Android自定义View】安卓制作自定义控件
- (android)自定义View自定义属性
- 自定义View(二)之自定义属性
- 自定义view(一)自定义属性
- 自定义View(一):自定义属性获取
- 安卓自定义view进阶(1)
- Android自定义View(自定义控件)
- 自定义view (一)自定义控件 viewGroup
- 安卓自定义View
- 安卓自定义view
- 安卓自定义View
- 超详细MySQL安装及基本使用教程
- Alibaba编码规约遵从(二)持续集成
- 音视频开发的一系列科普教程
- 达梦数据库存储过程调用
- Spring的AOP实现
- (安卓)自定义控件,自定义(view)中的自定义属性
- Java单例模式
- 使用git把本地文件放到github上去
- 所想
- HTML-1-基础练习
- 作业排程算法—动态规划实现
- [LeetCode] 650. 2 Keys Keyboard
- 八数码第四境界——暴力反向BFS+康托展开判重+打表+回溯记录路径
- 简书大V公然侮辱程序员群体低智商,低素质,恋尸癖!