Android Intent

在Java web开发里面,页面与页面之间跳转,可以使用超链接,或者用表单发出一个请求给服务器,然后服务器根据请求返回所需要的页面。而使用MVC开发里面,会有一个Control专门去管理这个跳转。

而对于android来说开发来说,android的每个Activity相当于web里面的页面。而每个Actvity不可能只是单独在工作,所以Activity之间的交互就显得非常重要。

下面模拟一下以前Java WEB开发的一个例子。

有一个登录的框,当用户填写完表单(这个表单包含username和password)之后提交到服务器,服务器返回一个页面,这个页面显示用户的username和password。

在Java web里面,页面之间的信息交换可以用requestsession

比如

username = request.getParameter("username") ;

也可以使用session

username = (String)session.getAttribute("username") ;

这是当时学习Java Web的例子。

今天,我们也用这个例子在android实现一下;

布局:

login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="fill_parent"
              android:layout_width="fill_parent"
              android:orientation="vertical">
    <TableLayout android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:stretchColumns="1">
        <TableRow android:layout_width="wrap_content"
                  android:layout_height="wrap_content">
        <TextView android:text="姓名:"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"/>
        <EditText android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:id="@+id/username"/>
        </TableRow>
        <TableRow android:layout_width="wrap_content"
                  android:layout_height="wrap_content">
        <TextView android:text="密码:"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"/>
        <EditText android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:id="@+id/password"
                  android:password="true"/>
        </TableRow>
    </TableLayout>
    <Button android:id="@+id/login"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="登陆"/>
</LinearLayout>

view.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/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

LoginActivity.java

package org.jian.intent;

import org.jian.R;

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

public class LoginActivity extends Activity {
    EditText username , password;    
    Button login ;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.login) ;    //布局
    	
    	username = (EditText)findViewById(R.id.username) ;
    	password = (EditText)findViewById(R.id.password) ;
    	login = (Button)findViewById(R.id.login) ;
    	
    	login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String _username = username.getText().toString() ;    //获取表单的信息
				String _password = password.getText().toString() ;    //
				
				Intent intent = new Intent() ;
				
				intent.setClass(LoginActivity.this, ShowActivity.class) ;//转移到ShowActivity
				//把信息保存在Extra中,然后在另一个activity中用getxxxExtra()获取
				intent.putExtra("_username", _username) ;               //传递Activity信息
				intent.putExtra("_password", _password) ;
				
				LoginActivity.this.startActivity(intent) ;
				LoginActivity.this.finish() ;
			}
		}) ;
   }
}

ShowActivity.java

package org.jian.intent;

import org.jian.R;

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

public class ShowActivity extends Activity {
     TextView text  ;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
       setContentView(R.layout.view) ;
       text = (TextView)findViewById(R.id.text) ;
       
       Intent intent = this.getIntent() ;     
       //获取上一个Activity的信息,相当于JavaWeb中的getParameter()
       String _username = intent.getStringExtra("_username") ;
       String _password = intent.getStringExtra("_password") ;
       
       text.setText("我的名字是:"+_username+"\n"+"我的密碼是:"+_password) ;
   }
}
AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="6"
        android:targetSdkVersion="7" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="org.jian.intent.ShowActivity"></activity>
        <activity android:name="org.jian.intent.LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

运行结果:




以前听说过Android与Java EE有很多相同,今天看来确实有这种感觉




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值