首页 > 学院 > 开发设计 > 正文

自定义View之捆绑控件

2019-11-08 01:26:18
字体:
来源:转载
供稿:网友

自定义控件是我一直都想学的,但是之前因为基础不牢固,看了很多大神的博客,都是一时激动,然后也只能激动,好多内部原理还是不懂,但是又不想错过自定义View,毕竟看起来比较吊;以前看爱哥的博客,(虽然还欠我们几篇)启发比较大,但是自己深入不了。后来因为别的事也就不了了之了。最近看别的博客又突然看到了一个最简单的自定义VIew,一下子勾起了心伤,于是自己就又捡起基础看起来,虽然阻挠比较大(师傅不让看,说是没到时候),深入的精髓就暂时不看,就看了简单的控件捆绑,等有时间再深入。

先上个图:

一共是两个View

先看第二个:登录那个

xml中:比较简单的布局,估计丑的都没人愿意看,还是贴一下吧:

xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/activity_login"    android:layout_width="match_parent"    android:layout_height="160dip"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="60dip"        android:padding="10dip">        <TextView            android:layout_width="wrap_content"            android:layout_height="40dip"            android:layout_marginRight="5dip"            android:gravity="center"            android:text="账号:"            android:textColor="#000000"            android:textSize="16sp" />        <EditText            android:id="@+id/account"            android:layout_width="0dip"            android:layout_height="40dip"            android:layout_marginLeft="5dip"            android:layout_weight="1"            android:background="@drawable/bg_login" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="60dip"        android:padding="10dip">        <TextView            android:layout_width="wrap_content"            android:layout_height="40dip"            android:layout_marginRight="5dip"            android:gravity="center"            android:text="密码:"            android:textColor="#000000"            android:textSize="16sp" />        <EditText            android:id="@+id/passWord"            android:layout_width="0dip"            android:layout_height="40dip"            android:layout_marginLeft="5dip"            android:layout_weight="1"            android:background="@drawable/bg_login" />    </LinearLayout>    <Button        android:id="@+id/login"        android:layout_width="match_parent"        android:layout_height="40dip"        android:layout_marginLeft="10dip"        android:layout_marginRight="10dip"        android:background="#800000ff"        android:onClick="Login"        android:text="登  录"        android:textColor="#ffffff"        android:textSize="16sp" /></LinearLayout>然后是java代码

Java:

/** * Created by xieyang on 2017/2/20. */public class LoginView extends LinearLayout {    PRivate EditText account;    private EditText password;    public interface Login {        void Login(View v);    }    private void init(Context context) {        LayoutInflater.from(context).inflate(R.layout.login1_activity, this);        account = (EditText) findViewById(R.id.account);        password = (EditText) findViewById(R.id.password);    }    public LoginView(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);    }    public String getAccount() {        return account.getText().toString().trim();    }    public String getPassword() {        return password.getText().toString().trim();    }}继承自LinearLayout,因为xml的根布局是LinearLayout

然后是调用;调用就简单了,设置个长宽,设置个id就可以了

第二个:

xml中:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/activity_register"    android:layout_width="match_parent"    android:layout_height="210dip"    android:orientation="vertical"    android:padding="10dip">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="40dip"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_marginRight="5dip"            android:gravity="center_vertical"            android:text="账号:"            android:textColor="#000000"            android:textSize="16sp" />        <EditText            android:id="@+id/r_account"            android:layout_width="0dip"            android:layout_height="match_parent"            android:layout_marginLeft="5dip"            android:layout_weight="1"            android:background="@drawable/bg_register"            android:hint="请输入账号"            android:padding="5dip" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="40dip"        android:layout_marginTop="10dip"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_marginRight="5dip"            android:gravity="center_vertical"            android:text="密码:"            android:textColor="#000000"            android:textSize="16sp" />        <EditText            android:id="@+id/r_password1"            android:layout_width="0dip"            android:layout_height="match_parent"            android:layout_marginLeft="5dip"            android:layout_weight="1"            android:background="@drawable/bg_register"            android:hint="请输入密码"            android:padding="5dip" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="40dip"        android:layout_marginTop="10dip"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_marginRight="5dip"            android:gravity="center_vertical"            android:text="密码:"            android:textColor="#000000"            android:textSize="16sp" />        <EditText            android:id="@+id/r_password2"            android:layout_width="0dip"            android:layout_height="match_parent"            android:layout_marginLeft="5dip"            android:layout_weight="1"            android:background="@drawable/bg_register"            android:gravity="center_vertical"            android:hint="再次确认密码"            android:padding="5dip" />    </LinearLayout>    <Button        android:layout_width="match_parent"        android:layout_height="40dip"        android:layout_marginTop="10dip"        android:background="@drawable/bg_button_select"        android:onClick="rLogin"        android:text="注   册"        android:textColor="#ffffff"        android:textSize="16sp" /></LinearLayout>Java代码中:

Java:

/** * Created by xieyang on 2017/2/21. */public class RegisterView extends LinearLayout {    private EditText r_account;    private EditText r_pasword1;    private EditText r_pasword2;    public interface RLogin {        void rLogin(View v);    }    private void init(Context context) {        LayoutInflater.from(context).inflate(R.layout.register_layout, this);        r_account = (EditText) findViewById(R.id.r_account);        r_pasword1 = (EditText) findViewById(R.id.r_password1);        r_pasword2 = (EditText) findViewById(R.id.r_password2);    }    public RegisterView(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);    }    public String getRAccount() {        return r_account.getText().toString().trim();    }    public String getRPassword1() {        return r_pasword1.getText().toString().trim();    }    public String getRPassword2() {        return r_pasword2.getText().toString().trim();    }}XML中一些背景什么的:

注册那个编辑框背景:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <corners android:radius="10dip" />    <stroke        android:width="2dip"        android:color="#34bbaa" /></shape>注册按钮点击变色:selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/bg_button" android:state_enabled="true" android:state_pressed="true" />    <item android:drawable="@drawable/bg_button" android:state_enabled="true" android:state_focused="true" />    <item android:drawable="@drawable/bg_button_un" android:state_enabled="false" android:state_focused="false" /></selector>按钮没有点击时:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <corners android:radius="10dip"/>    <solid android:color="#5034bbaa"/></shape>按钮点击时:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <corners android:radius="10dip"/>    <solid android:color="#34bbaa"/></shape>差不多就这样,还有一个接口按钮点击的,那个我发现不定义然后MainActivity中调用那个方法也可以,然后就不太懂到底有没有用了

以后有时间再去学自定义View,可以设置属性的那种,那才牛逼啊


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表