自定义复杂组件-登录组件模块

<span style="font-size:32px;">目前很多上线项目都需要登录这个功能模块,那么能不能将登录木块这部分的UI封装成一个组件直接调用了,自己写了一下这是可以的。</span>

直接上源码:

源码地址:http://download.csdn.net/detail/u013922681/8943743

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
LoginView.java 登录模块,自定义成一个组件,直接拿来使用

package com.example.custem03;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LoginView extends LinearLayout {

	private TextView tvUserName;
	private TextView tvPassword;
	private EditText etUserName;
	private EditText etPassword;
	private Button btnLogin;
	private View view;
	public LoginView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view =  inflater.inflate(R.layout.login, this);
	}
	
	
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		System.out.println("onFinishInflate");
		tvUserName = (TextView) view.findViewById(R.id.activity_login_tv_username);
		tvPassword = (TextView) view.findViewById(R.id.activity_login_tv_password);
		etUserName = (EditText) view.findViewById(R.id.activity_login_et_username);
		etPassword = (EditText) view.findViewById(R.id.activity_login_et_password);
		btnLogin = (Button) view.findViewById(R.id.activity_login_btn_login);
		btnLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getContext(), "test", 1).show();
			}
		});
		
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		System.out.println("onSizeChanged");
	}
}

login.xml 登录界面的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <LinearLayout 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal" >
		<TextView 
		    android:id="@+id/activity_login_tv_username"
		    android:layout_weight="1"
		    android:gravity="center_horizontal"
		    android:layout_width="0dp"
		    android:layout_height="wrap_content"
		    android:text="用户名:"/>
		<EditText 
		    android:id="@+id/activity_login_et_username"
		    android:layout_weight="3"
		    android:layout_width="0dp"
		    android:layout_height="wrap_content"
		    android:hint="请输入用户名"/>
	</LinearLayout>
    
    <View 
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"/>
    
    <LinearLayout 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal" >
		<TextView 
		    android:id="@+id/activity_login_tv_password"
		    android:layout_weight="1"
		    android:gravity="center_horizontal"
		    android:layout_width="0dp"
		    android:layout_height="wrap_content"
		    android:text="密码:"/>
		<EditText 
		    android:id="@+id/activity_login_et_password"
		    android:layout_weight="3"
		    android:layout_width="0dp"
		    android:layout_height="wrap_content"
		    android:hint="请输入密码"/>
	</LinearLayout>
    
    <View 
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"/>
    
    <Button 
        android:id="@+id/activity_login_btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:background="@drawable/activity_login_btn"/>

</LinearLayout>
activity_main.xml主界面布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.example.custem03.LoginView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

activivity_login_btn.xml  按钮背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="2dp" />

    <padding
        android:bottom="3dp"
        android:top="3dp" />

    <solid android:color="#fff" />

    <stroke
        android:width="2px"
        android:color="#000" />

</shape>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值