Android学习笔记(6)——Android——LoginDemo

在这个demo中,将涉及到Activity(活动)的交互——从一个屏幕到另一个屏幕,通过Intent来实现的……

 

工程目录结构:

 

    LoginDemoActivity程序清单

package com.oristand;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class LoginDemoActivity extends Activity {

	// 点击go按钮,进入登录活动(LoginActivity)
	private Button btn_go;

	// 按钮添加监听事件
	private BtnListener btnListener = new BtnListener();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 丛当前试图中找到按钮,初始化
		btn_go = (Button) findViewById(R.id.btn_go);

		// 绑定点击事件
		btn_go.setOnClickListener(btnListener);

	}

	// 监听事件
	private class BtnListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.btn_go) {
				login();
			}
		}
	}
                // 通过Intent实现跳转
	public void login() {
		Intent intent_login = new Intent();
		intent_login.setClass(this, LoginActivity.class);
		startActivity(intent_login);
	}
}

 

    LoginActivity.java程序清单

package com.oristand;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {

	// 用户名输入框
	private EditText et_username;

	// 密码输入框
	private EditText et_passwd;

	// 登录按钮
	private Button btn_login;

	// 取消按钮
	Button btn_reset;

	// 按钮添加点击事件
	BtnListener btnListener = new BtnListener();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);

		// 设置当前视图
		setContentView(R.layout.login);

		// 从当前事件中找到输入框,初始化
		et_username = (EditText) findViewById(R.id.et_username);
		et_passwd = (EditText) findViewById(R.id.et_passwd);

		// 从当前事件中找到登录按钮,初始化
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(btnListener);

		// ...
		btn_reset = (Button) findViewById(R.id.btn_reset);
		btn_reset.setOnClickListener(btnListener);
	}

	// 点击事件
	private class BtnListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.btn_login) {

				// do login...

			} else if (v.getId() == R.id.btn_reset) {
				finish();
			}
		}
	}
}

 

  R.java程序清单

package com.oristand;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int btn_go=0x7f050004;
        public static final int btn_login=0x7f050002;
        public static final int btn_reset=0x7f050003;
        public static final int et_passwd=0x7f050001;
        public static final int et_username=0x7f050000;
    }
    public static final class layout {
        public static final int login=0x7f030000;
        public static final int main=0x7f030001;
    }
    public static final class string {
        public static final int app_login=0x7f040002;
        public static final int app_name=0x7f040000;
        public static final int txt_btn_go=0x7f040001;
        public static final int txt_btn_login=0x7f040005;
        public static final int txt_btn_reset=0x7f040006;
        public static final int txt_passwd=0x7f040004;
        public static final int txt_username=0x7f040003;
    }
}

 

   main.xml程序清单

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal">
	<Button android:id="@+id/btn_go" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="@string/txt_btn_go" />
</LinearLayout>

	

 

   login.xml程序清单

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

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

		<TextView android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:text="@string/txt_username" />

		<EditText android:id="@+id/et_username" android:maxLength="8"
			android:maxLines="1" android:layout_weight="1.0"
			android:layout_width="fill_parent" android:layout_height="wrap_content"
			android:text="" />
	</TableRow>

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

		<TextView android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:text="@string/txt_passwd" />

		<EditText android:id="@+id/et_passwd" android:password="true"
			android:maxLength="10" android:maxLines="1" android:layout_weight="1.0"
			android:layout_width="fill_parent" android:layout_height="wrap_content"
			android:text="" />
	</TableRow>
	<LinearLayout android:orientation="horizontal"
		android:layout_width="fill_parent" android:layout_height="wrap_content">

		<Button android:id="@+id/btn_login" android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:layout_weight="1.0"
			android:text="@string/txt_btn_login" />

		<Button android:id="@+id/btn_reset" android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:layout_weight="1.0"
			android:text="@string/txt_btn_reset" />
	</LinearLayout>
</TableLayout>

 

   string.xml程序清单

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="app_name">LoginDemo</string>
	<string name="txt_btn_go">Go...</string>

	<string name="app_login">登录</string>
	<string name="txt_username">用户名:</string>
	<string name="txt_passwd">密 码:</string>
	<string name="txt_btn_login">登 录</string>
	<string name="txt_btn_reset">取 消</string>
</resources>

  

   AndroidManifest.xml程序清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.oristand" android:versionCode="1" android:versionName="1.0.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".LoginDemoActivity" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<!-- login activity -->
		<activity android:name=".LoginActivity" android:label="@string/app_login"></activity>
	</application>
</manifest> 

   

   程序运行配置:

 

default就可以……

AndroidManifest.xml中的配置可以知道答案……就相当与函数的入口是main方法一样

...

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
...

运行效果:

      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值