实验二:android实现登录功能

实现方法:

用Intent实现两个Activity(分别为LoginActivity和usrandpasswd)之间的切换和数据传递。
用onActivityResult()函数实现在父Activity中处理子Activity的返回值。
LoginActivity的布局在main.XML中;usrandpasswd的布局在passwd.XML中。

代码:
LoginActivity.java:

package com.gaofeifei.login;

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

public class LoginActivity extends Activity {
	final int ACTIVITY1 = 1;
	private TextView showResult;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showResult   = (TextView) findViewById(R.id.showResult);
        Button login = (Button) findViewById(R.id.login);
        
        login.setOnClickListener(new View.OnClickListener() {
			
		
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				
				intent.setClass(LoginActivity.this, usrandpasswd.class);
				
				startActivityForResult(intent, ACTIVITY1);
			}
		});
    }


	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		switch(requestCode)
		{
		case ACTIVITY1:
			if(resultCode == RESULT_OK)
				showResult.setText(data.getData().toString());
			
			break;
		}
    }
}

usrandpasswd.java:

package com.gaofeifei.login;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class usrandpasswd extends Activity {

	private EditText usrname;
	private EditText passwd;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

        setContentView(R.layout.passwd);
        
        
        usrname = (EditText) findViewById(R.id.usrnameEditText);
        passwd  = (EditText) findViewById(R.id.passwdEditText);
        
        Button button = (Button) findViewById(R.id.passwdButton);
        
        button.setOnClickListener(new View.OnClickListener() {
			
	
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String msg = "用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();
				Intent result = new Intent(null, Uri.parse(msg));
				
				setResult(RESULT_OK, result);
				
				finish();
			}
		});
	}
}

activity_main.xml:


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

    <TextView
        android:id="@+id/showResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="登陆" />

LinearLayout>

passwd.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/usrnameTextView"
        android:layout_width="90dip"
        android:layout_height="wrap_content"
        android:text="用户名:"
        android:textSize="18dip" />

    <EditText
        android:id="@+id/usrnameEditText"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:layout_below="@id/usrnameTextView"
        android:text="" />

    <TextView
        android:id="@+id/passwdTextView"
        android:layout_width="90dip"
        android:layout_height="wrap_content"
        android:layout_below="@id/usrnameEditText"
        android:text="密码:"
        android:textSize="18dip" />

    <EditText
        android:id="@+id/passwdEditText"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:layout_below="@id/passwdTextView"
        android:text="" />

    <Button
        android:id="@+id/passwdButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/passwdEditText"
        android:layout_marginTop="15dip"
        android:text="确认"
        android:textSize="18dip" />

RelativeLayout>

AndroidManifest.xml:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gaofeifei.login"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
        <activity
            android:name=".usrandpasswd"
            android:label="@string/app_name" >
        activity>
    application>

manifest>

结果:
实验二:android实现登录功能_第1张图片
实验二:android实现登录功能_第2张图片

你可能感兴趣的:(android)