EditText实现大众(登录/注册)输入框,TextView实现仿CheckBox效果-脚本宝典
我想很多人看到上图红框中的部分的实现第一反应就是使用CheckBox控件来实现,博主在一开始也是这么去做的,但是后来用不同的手机去做测试的时候发现checkbox对适配表现的很差劲,文字与图片的距离很不好控制,后来项目组长告诉我直接用去TextView去实现,试了才知道其实用TextView去实现很简单。代码:
viewheader.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout >"http://schemas.android.com/apk/res/android"
android:id="@+id/rl_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_homepage_header" >

<LinearLayout
    android:id="@+id/ll_header_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10sp" >

    <Button
        android:id="@+id/btn_header_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_header_back" />
</LinearLayout>

<TextView
    android:id="@+id/tv_header_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:textColor="@color/white"
    android:textSize="25sp" />

 </RelativeLayout>

activity_register.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout >"http://schemas.android.com/apk/res/android"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_background="@drawable/bg_homepage_middle"
android_orientation="vertical" >

<include layout="@layout/view_header" /><!--项目中经常用到include控件,减少重复代码-->

<LinearLayout
    android_layout_width="match_parent"
    android_layout_height="wrap_content"
    android_orientation="vertical"
    android_paddingLeft="13dp"
    android_paddingRight="13dp"
    android_paddingTop="13dp" >

    <RelativeLayout
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_background="@drawable/bg_complaint_username" >  <!--后来想想其实把这个属性放替换到EditText中也是可以实现这个效果的,并且还少了层布局-->

        <EditText
            android_id="@+id/edt_register_username"
            android_layout_width="fill_parent"
            android_layout_height="wrap_content"
            android_layout_centerVertical="true"
            android_layout_marginLeft="15sp"
            android_background="@null"
            android_drawableLeft="@drawable/icon_username"
            android_drawablePadding="20sp"      <!-- 用于控制EditText上图片与文字之间的距离-->
            android_ems="8"
            android_hint="@string/edt_register_name"
            android_textSize="15sp"
            android_inputType="text"
            android_singleLine="true" />
    </RelativeLayout>

    <ImageView                            <!--上下EditText之间的分割线-->
        android_id="@+id/divider_1"
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_background="@drawable/bg_complaint_divider"
        android_contentDescription="@null" />

    <RelativeLayout
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_background="@drawable/bg_complaint_phone" >

        <EditText
            android_id="@+id/edt_register_phone"
            android_layout_width="fill_parent"
            android_layout_height="wrap_content"
            android_layout_centerVertical="true"
            android_layout_marginLeft="15sp"
            android_layout_marginRight="15sp"
            android_background="@null"
            android_drawableLeft="@drawable/icon_phone"
            android_drawablePadding="20sp"
            android_ems="8"
            android_textSize="15sp"
            android_hint="@string/hint_your_phone"
            android_inputType="phone"
            android_singleLine="true" />
    </RelativeLayout>

    <ImageView
        android_id="@+id/divider_2"
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_background="@drawable/bg_complaint_divider"
        android_contentDescription="@null" />

    <RelativeLayout
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_background="@drawable/bg_complaint_platenumber" >

        <EditText
            android_id="@+id/edt_register_password"
            android_layout_width="fill_parent"
            android_layout_height="wrap_content"
            android_layout_centerVertical="true"
            android_layout_marginLeft="15sp"
            android_layout_marginRight="15sp"
            android_background="@null"
            android_drawableLeft="@drawable/icon_password"
            android_drawablePadding="20sp"
            android_ems="8"
            android_textSize="15sp"
            android_hint="@string/tv_register_password_1"
            android_inputType="textPassword"
            android_singleLine="true" />
    </RelativeLayout>

    <ImageView
        android_id="@+id/divider_3"
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_background="@drawable/bg_complaint_divider"
        android_contentDescription="@null" />

    <RelativeLayout
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_background="@drawable/bg_complaint_4s" >

        <EditText
            android_id="@+id/edt_user_repassword"
            android_layout_width="fill_parent"
            android_layout_height="wrap_content"
            android_layout_centerVertical="true"
            android_layout_marginLeft="15sp"
            android_layout_marginRight="15sp"
            android_background="@null"
            android_drawableLeft="@drawable/icon_password"
            android_drawablePadding="20sp"
            android_ems="8"
            android_textSize="15sp"
            android_hint="@string/tv_register_repassword"
            android_inputType="textPassword"
            android_singleLine="true" />
    </RelativeLayout>

    <RelativeLayout
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_layout_marginTop="16dp" >

        <RelativeLayout
            android_id="@+id/rl_edt_code"
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_background="@drawable/bg_edt_code" >

            <EditText
                android_id="@+id/edt_verify_code"
                android_layout_width="fill_parent"
                android_layout_height="wrap_content"
                android_layout_centerVertical="true"
                android_layout_marginLeft="15sp"
                android_layout_marginRight="15sp"
                android_background="@null"
                android_drawableLeft="@drawable/icon_code"
                android_drawablePadding="20sp"
                android_ems="8"
                android_textSize="15sp"
                android_hint="@string/edt_register_code"
                android_inputType="text"
                android_singleLine="true" />
        </RelativeLayout>

        <Button
            android_id="@+id/get_verify_code"
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_layout_alignParentRight="true"
            android_background="@drawable/bg_btn_getcode"
            android_text="@string/btn_register_code"
            android_textColor="@color/steeblue2" />
    </RelativeLayout>

    <LinearLayout
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_layout_marginTop="20dp"
        android_orientation="horizontal"
        android_padding="8dp" >

        <TextView
            android_id="@+id/check_if_read"
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_textSize="13sp"
            android_drawableLeft="@drawable/bg_if_read_yes"  <!--TextView中仍然可以用drawablePadding来控制图片与文本之间的距离-->
            android_clickable="true"   <!--事实证明这个属性在这里可有可无-->
            android_text="@string/tv_if_read" />

        <TextView
            android_id="@+id/tv_article"
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_text="@string/tv_article"
            android_textSize="14sp"
            android_textColor="@color/steelblue" />
    </LinearLayout>

    <Button
        android_layout_marginTop="30dp"
        android_id="@+id/btn_submit"
        android_layout_width="match_parent"
        android_layout_height="wrap_content" 
        android_text="@string/btn_determine"
        android_textColor="@color/white"
        android_background="@drawable/bg_register_submit"
        />
</LinearLayout>

</LinearLayout>

RegisterActivity.java:

public class RegisterActivity extends Activity {
private TextView tv_header;
private TextView tv_check;
private Button btn_back;
private boolean flag = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    initHeader();
}

private void initHeader() {
    tv_header = (TextView) findViewById(R.id.tv_header_title);
    tv_check = (TextView) findViewById(R.id.check_if_read);
    tv_header.setText(R.string.btn_register);
    tv_check.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (flag == false) {
                tv_check.setCompoundDrawablesWithIntrinsicBounds(          
                        R.drawable.bg_if_read_yes, 0, 0, 0);  //这个方法和Drawablexxx属性对应用于动态设置TextView的drawable
                flag = true;
            }else if(flag==true){
                tv_check.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bg_if_read_blank,0,0, 0);
                flag=false;
            }
        }

    });
    btn_back = (Button) findViewById(R.id.btn_header_left);

    btn_back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    });

}

}
Ok就是这些啦

本文固定链接: http://www.js-code.com/android/android_56403.html