《黑马程序员》 安卓登录小案例

本文介绍了一个简单的Android登录界面布局设计,包括用户名与密码输入框、登录与退出按钮,并提供了相应的Activity及事件处理代码。

布局文件如下:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="200dip"
        android:orientation="vertical"
        android:padding="10dip" >

        <!-- 标题 -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="登录测试"
                android:textSize="28sp" />
        </LinearLayout>
        <!-- 内容 -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:gravity="center"
                    android:text="用户名"
                    android:textSize="18sp" />

                <EditText
                    android:id="@+id/l_uname"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="您的用户名..."
                    android:textSize="18sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:gravity="center"
                    android:text="密码"
                    android:textSize="18sp" />

                <EditText
                    android:id="@+id/l_pwd"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="您的密码..."
                    android:password="true"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
        <!-- 按钮 -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="e_login"
                android:text="登录" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="e_exit"
                android:text="退出" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

布局效果图:

  

activity及事件代码如下:

import android.app.Activity;
import android.os.Bundle;
import android.os.Process;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	//定义控件
	private EditText et_username, et_userpwd;

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

		// 初始化控件
		et_username = (EditText) this.findViewById(R.id.l_uname);
		et_userpwd = (EditText) this.findViewById(R.id.l_pwd);
	}

	//登录功能的事件代码
	public void e_login(View v) {
		// 获取用户名和密码
		String userName = et_username.getText().toString().trim();
		String userPwd = et_userpwd.getText().toString().trim();
		if (TextUtils.isEmpty(userName) | TextUtils.isEmpty(userPwd)) {
			// 如果用户名或密码为空
			// 有一个为假则为假使用|,有一个为假即为假
			Toast.makeText(this, "用户名和密码不能为空", 0).show();
		} else if ("heima".equals(userName) && "88888".equals(userPwd)) {
			Toast.makeText(this, "登录成功", 0).show();
		} else
			Toast.makeText(this, "用户名或密码不对", 0).show();
	}
	
	//退出事件的代码 
	public void e_exit(View v){
		//如何自杀了,先获取到进程对象然后使用自杀的方式将其自杀 
		Process p=new Process();
		//如何得到当前应用的进程的id
		p.killProcess(Process.myPid());
	}

}

运行之后的效果图片:

  


  今天是上班的最后一天了。做这个小案例。以示怀念。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值