`
xiangxingchina
  • 浏览: 506445 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多

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

 

工程目录结构:

 

    LoginDemoActivity程序清单

Logindemoactivity 代码
  1. package com.oristand;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. public class LoginDemoActivity extends Activity {  
  10.   
  11.     // 点击go按钮,进入登录活动(LoginActivity)  
  12.     private Button btn_go;  
  13.   
  14.     // 按钮添加监听事件  
  15.     private BtnListener btnListener = new BtnListener();  
  16.   
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.   
  23.         // 丛当前试图中找到按钮,初始化  
  24.         btn_go = (Button) findViewById(R.id.btn_go);  
  25.   
  26.         // 绑定点击事件  
  27.         btn_go.setOnClickListener(btnListener);  
  28.   
  29.     }  
  30.   
  31.     // 监听事件  
  32.     private class BtnListener implements View.OnClickListener {  
  33.         @Override  
  34.         public void onClick(View v) {  
  35.             // TODO Auto-generated method stub  
  36.             if (v.getId() == R.id.btn_go) {  
  37.                 login();  
  38.             }  
  39.         }  
  40.     }  
  41.                 // 通过Intent实现跳转  
  42.     public void login() {  
  43.         Intent intent_login = new Intent();  
  44.         intent_login.setClass(this, LoginActivity.class);  
  45.         startActivity(intent_login);  
  46.     }  
  47. }  
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程序清单

Loginactivity.java代码
  1. package com.oristand;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8.   
  9. public class LoginActivity extends Activity {  
  10.   
  11.     // 用户名输入框  
  12.     private EditText et_username;  
  13.   
  14.     // 密码输入框  
  15.     private EditText et_passwd;  
  16.   
  17.     // 登录按钮  
  18.     private Button btn_login;  
  19.   
  20.     // 取消按钮  
  21.     Button btn_reset;  
  22.   
  23.     // 按钮添加点击事件  
  24.     BtnListener btnListener = new BtnListener();  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         // TODO Auto-generated method stub  
  29.   
  30.         super.onCreate(savedInstanceState);  
  31.   
  32.         // 设置当前视图  
  33.         setContentView(R.layout.login);  
  34.   
  35.         // 从当前事件中找到输入框,初始化  
  36.         et_username = (EditText) findViewById(R.id.et_username);  
  37.         et_passwd = (EditText) findViewById(R.id.et_passwd);  
  38.   
  39.         // 从当前事件中找到登录按钮,初始化  
  40.         btn_login = (Button) findViewById(R.id.btn_login);  
  41.         btn_login.setOnClickListener(btnListener);  
  42.   
  43.         // ...  
  44.         btn_reset = (Button) findViewById(R.id.btn_reset);  
  45.         btn_reset.setOnClickListener(btnListener);  
  46.     }  
  47.   
  48.     // 点击事件  
  49.     private class BtnListener implements View.OnClickListener {  
  50.         @Override  
  51.         public void onClick(View v) {  
  52.             // TODO Auto-generated method stub  
  53.             if (v.getId() == R.id.btn_login) {  
  54.   
  55.                 // do login...  
  56.   
  57.             } else if (v.getId() == R.id.btn_reset) {  
  58.                 finish();  
  59.             }  
  60.         }  
  61.     }  
  62. }  
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程序清单

R.java代码
  1. package com.oristand;  
  2.   
  3. public final class R {  
  4.     public static final class attr {  
  5.     }  
  6.     public static final class drawable {  
  7.         public static final int icon=0x7f020000 ;  
  8.     }  
  9.     public static final class id {  
  10.         public static final int btn_go=0x7f050004 ;  
  11.         public static final int btn_login=0x7f050002 ;  
  12.         public static final int btn_reset=0x7f050003 ;  
  13.         public static final int et_passwd=0x7f050001 ;  
  14.         public static final int et_username=0x7f050000 ;  
  15.     }  
  16.     public static final class layout {  
  17.         public static final int login=0x7f030000 ;  
  18.         public static final int main=0x7f030001 ;  
  19.     }  
  20.     public static final class string {  
  21.         public static final int app_login=0x7f040002 ;  
  22.         public static final int app_name=0x7f040000 ;  
  23.         public static final int txt_btn_go=0x7f040001 ;  
  24.         public static final int txt_btn_login=0x7f040005 ;  
  25.         public static final int txt_btn_reset=0x7f040006 ;  
  26.         public static final int txt_passwd=0x7f040004 ;  
  27.         public static final int txt_username=0x7f040003 ;  
  28.     }  
  29. }  
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程序清单

Main.xml代码
  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:orientation="vertical"  android:layout_width= "fill_parent"   
  4.     android:layout_height="fill_parent"  android:gravity= "center_vertical|center_horizontal" >  
  5.     <Button android:id="@+id/btn_go"  android:layout_width= "wrap_content"   
  6.         android:layout_height="wrap_content"  android:text= "@string/txt_btn_go"  />  
  7. </LinearLayout>  
  8.   
  9.       
<?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程序清单

Login.xml代码
  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:orientation="vertical"  android:layout_width= "fill_parent"   
  4.     android:layout_height="fill_parent" >  
  5.   
  6.     <TableRow android:orientation="horizontal"   
  7.         android:layout_width="fill_parent"  android:layout_height= "wrap_content" >  
  8.   
  9.         <TextView android:layout_width="fill_parent"   
  10.             android:layout_height="wrap_content"  android:text= "@string/txt_username"  />  
  11.   
  12.         <EditText android:id="@+id/et_username"  android:maxLength= "8"   
  13.             android:maxLines="1"  android:layout_weight= "1.0"   
  14.             android:layout_width="fill_parent"  android:layout_height= "wrap_content"   
  15.             android:text=""  />  
  16.     </TableRow>  
  17.   
  18.     <TableRow android:orientation="horizontal"   
  19.         android:layout_width="fill_parent"  android:layout_height= "wrap_content" >  
  20.   
  21.         <TextView android:layout_width="fill_parent"   
  22.             android:layout_height="wrap_content"  android:text= "@string/txt_passwd"  />  
  23.   
  24.         <EditText android:id="@+id/et_passwd"  android:password= "true"   
  25.             android:maxLength="10"  android:maxLines= "1"  android:layout_weight= "1.0"   
  26.             android:layout_width="fill_parent"  android:layout_height= "wrap_content"   
  27.             android:text=""  />  
  28.     </TableRow>  
  29.     <LinearLayout android:orientation="horizontal"   
  30.         android:layout_width="fill_parent"  android:layout_height= "wrap_content" >  
  31.   
  32.         <Button android:id="@+id/btn_login"  android:layout_width= "fill_parent"   
  33.             android:layout_height="wrap_content"  android:layout_weight= "1.0"   
  34.             android:text="@string/txt_btn_login"  />  
  35.   
  36.         <Button android:id="@+id/btn_reset"  android:layout_width= "fill_parent"   
  37.             android:layout_height="wrap_content"  android:layout_weight= "1.0"   
  38.             android:text="@string/txt_btn_reset"  />  
  39.     </LinearLayout>  
  40. </TableLayout>  
<?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程序清单

String.xml代码
  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <resources>  
  3.     <string name="app_name" >LoginDemo</string>  
  4.     <string name="txt_btn_go" >Go...</string>  
  5.   
  6.     <string name="app_login" >登录</string>  
  7.     <string name="txt_username" >用户名:</string>  
  8.     <string name="txt_passwd" >密 码:</string>  
  9.     <string name="txt_btn_login" >登 录</string>  
  10.     <string name="txt_btn_reset" >取 消</string>  
  11. </resources>  
<?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程序清单

Androidmanifest.xml代码
  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     package="com.oristand"  android:versionCode= "1"  android:versionName= "1.0.0" >  
  4.     <application android:icon="@drawable/icon"  android:label= "@string/app_name" >  
  5.         <activity android:name=".LoginDemoActivity"  android:label= "@string/app_name" >  
  6.             <intent-filter>  
  7.                 <action android:name="android.intent.action.MAIN"  />  
  8.                 <category android:name="android.intent.category.LAUNCHER"  />  
  9.             </intent-filter>  
  10.         </activity>  
  11.         <!-- login activity -->  
  12.         <activity android:name=".LoginActivity"  android:label= "@string/app_login" ></activity>  
  13.     </application>  
  14. </manifest>   
<?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>
...

运行效果:

      

分享到:
评论

相关推荐

    LoginDemo_android_源码

    安卓 登录注册小模块,简单实现了前后端分离

    logindemo.zip

    SpringBoot整合swagger2、spring-security进行权限验证

    LoginDemo-master.

    LoginDemo-master 不成功的,可以直接在这里下载速来速来

    LoginDemo.zip

    LoginDemo QQ登录验证的小例子 客户端: 1、登录时检查网络状态 2、登录加载进度条 3、登录服务器端进行验证,如果用户名和密码存在且正确,则登录,否则失败 4、注册时将用户信息保存到服务器端数据库中(MySQL)...

    LoginDemo1.zip

    LoginDemo1.zip

    loginDemo

    loginDemo

    基于S2SH的LoginDemo

    基于S2SH框架 MySQL数据库 MyEclipse平台下开发的一个简单的LoginDemo程序,主要是为了熟悉S2SH框架的搭建流程,建议读者按照里面的PPT手动自行搭建,同时配有数据库文件。

    SSH架构搭建--LoginDemo.ppt

    SSH架构搭建--LoginDemo.ppt

    LoginDemo_part1

    MVC MVP MVVM

    ASpectJ_LoginDemo.zip

    AspectJ的演示代码,配合blog食用更佳!

    LoginDemo_MVVM.zip

    C# WPF+MVVM 实现登录跳转功能,适合初学者

    SSH架构搭建示例LoginDemo

    这是有关怎样在电脑上成功配置框架ssh这是有关怎样在电脑上成功配置框架ssh这是有关怎样在电脑上成功配置框架ssh这是有关怎样在电脑上成功配置框架ssh这是有关怎样在电脑上成功配置框架ssh这是有关怎样在电脑上成功...

    miniTwitterLoginDemo_Android实现Twitter网站迷你登录界面.rar

    miniTwitterLoginDemo_Android实现Twitter网站迷你登录界面.rar

    Oracle LoginDemo

    用c#连接Oracle的小程序,里面有相应的代码和数据

    day08_LoginDemo.zip

    本案例是Tomcat+Servlet+JDBC的一个小的案例,通过学习登录的逻辑来熟悉知识点。本案例是Tomcat+Servlet+JDBC的一个小的案例,通过学习登录的逻辑来熟悉知识点。

    loginDemo.rar

    实现了javaweb登录功能,而且还有实现验证码功能,这是一个小案例,可以看看,多学习学习,有助于javaweb技术的理解

Global site tag (gtag.js) - Google Analytics