Android—开发自学历程(1)-Intent-第二个Activity

来源:互联网 发布:程序员简历 markdown 编辑:程序博客网 时间:2024/06/16 22:59

现在市面上有非常多的app,我们使用它们,就会发现它们拥有非常多的界面,所以我们发现开发android其实不当当只有一个Activity,它应该是有许多的Activity组成。所以我接下来学习第二个Activity。


1.1 Intent

Intent的继承关系;
- java.lang.Object
↳ android.content.Intent

Intent中文意思是“意图;目的”;在android中Intent是负责应用之间的交互与通信。Intent负责操作一次动作、数据传递;Intent可以使用 startActivity打开一个Activity;broadcastIntent可以发送一个广播;还用来启动服务(startService(Intent), bindService(Intent, ServiceConnection, int) );总之他是很有用的。


接下来我们看看具体的用法。

首先先注册Activity。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.androidintentjoin"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="15"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            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=".SecondActivity"            android:label="@string/second_name" >        </activity>    </application></manifest>

注册Activity就是告诉android系统我们有这个,好让系统能调用它,因为但我们调用startActivity(…)时,调用请求实际发给了操作系统的ActivityManager,ActivityManager负责创建Activity实例并调用其onCreate(…)方法。


第一个布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:layout_gravity="center" >    <LinearLayout       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:orientation="vertical"  >        <EditText             android:id="@+id/user_name"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="@string/user_label"/>        <EditText             android:id="@+id/pass_word"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:inputType="textPassword"/>    </LinearLayout>    <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="right"        android:orientation="horizontal">        <Button             android:id="@+id/Login_in"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/login_label"/>        <Button             android:id="@+id/into_in"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/into_label"/>        <Button             android:id="@+id/cancel"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/cancel_label"/>    </LinearLayout></LinearLayout>

第二个布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:gravity="center" >    <EditText        android:id="@+id/uesrname_text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="@string/this_username"/>    <EditText        android:id="@+id/password_text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="@string/this_password"/>    <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:gravity="right">        <Button             android:id="@+id/alter_information"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/alter_label"/>    </LinearLayout></LinearLayout>

java 源码:

package com.example.androidintentjoin;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    private String userName = "Marisa";    private String passWord = "123456789";    private String username = null;    private String password = null;    private EditText mUserName = null;    private EditText mPassWord = null;    private Button mLogIn = null;    private Button mInto = null;    private Button mCancel = null;    private void initWidget() {        mUserName = (EditText) findViewById(R.id.user_name);        mPassWord = (EditText) findViewById(R.id.pass_word);        mLogIn = (Button) findViewById(R.id.Login_in);        mInto = (Button) findViewById(R.id.into_in);        mCancel = (Button) findViewById(R.id.cancel);    }    private void updataListener() {        mLogIn.setOnClickListener(new myLogInListener());        mInto.setOnClickListener(new myIntoListener());        mCancel.setOnClickListener(new myCancelListener());    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initWidget();        updataListener();    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        switch (resultCode) {        case RESULT_OK://第二个Activity设置的请求代码            Bundle b = data.getExtras();            username = b.getString("username");            userName = b.getString("username");            password = b.getString("password");            passWord = b.getString("password");            break;        default:            break;        }    }    private class myLogInListener implements View.OnClickListener{        @Override        public void onClick(View v) {            String use = userName;            String pass = passWord;            if (mUserName.getText().toString().equals(userName)&&                    mPassWord.getText().toString().equals(passWord)) {                Intent intent = new Intent(MainActivity.this, SecondActivity.class);                Bundle bundle = new Bundle();                bundle.putString("usename", use);                bundle.putString("password", pass);                intent.putExtras(bundle);                startActivityForResult(intent, 0);            }else if(!mUserName.getText().toString().equals(userName)) {                Toast.makeText(MainActivity.this, "账号错误", Toast.LENGTH_SHORT).show();            }else if (!mPassWord.getText().toString().equals(passWord)) {                Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();            }        }    }    private class myIntoListener implements View.OnClickListener{        @Override        public void onClick(View v) {            Intent intent = new Intent(MainActivity.this, SecondActivity.class);            Bundle bundle = new Bundle();            bundle.putString("usename", username);            bundle.putString("password", password);            intent.putExtras(bundle);            startActivityForResult(intent, 0);        }    }    private class myCancelListener implements View.OnClickListener{        @Override        public void onClick(View v) {            mUserName.setText("");            mPassWord.setText("");        }    }}

这里使用了Intent和Bundle
Intent负责传值,而bundle负责将多个值放在一起。
这里使用startActivityForResult(intent, 0);(第一个是Intent,第二个是请求代码)。
如果不接收返回数据可以使用startActivity(Intent intent);
new Intent(当前Activity,目标Activity);
Bundle.putString(key,values);是一种键值对存储机制
protected void onActivityResult(int requestCode, int resultCode, Intent data)用于处理返回数据。


package com.example.androidintentjoin;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class SecondActivity extends Activity {    private EditText mUserText = null;    private EditText mPasswordText = null;    private Button mAfter = null;    private Bundle mBundle = null;    private void initWidget() {        mUserText = (EditText) findViewById(R.id.uesrname_text);        mPasswordText = (EditText) findViewById(R.id.password_text);        mAfter = (Button) findViewById(R.id.alter_information);         mBundle = new Bundle();    }    private void initIntent() {        mBundle = getIntent().getExtras();        String use = mBundle.getString("usename");        String password = mBundle.getString("password");        mUserText.setText(use);        mPasswordText.setText(password);    }    private void setAfterData() {        Intent intent = new Intent();        Bundle bundle = new Bundle();        String use = mUserText.getText().toString();        String pass = mPasswordText.getText().toString();        bundle.putString("username", use);        bundle.putString("password", pass);        intent.putExtras(bundle);        setResult(RESULT_OK, intent);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        initWidget();        initIntent();        mAfter.setOnClickListener(new OnClickListener() {                   @Override            public void onClick(View v) {                if (TextUtils.isEmpty(mUserText.getText())||                        TextUtils.isEmpty(mPasswordText.getText())) {                    Toast.makeText(SecondActivity.this, "无数据不能修改", Toast.LENGTH_SHORT).show();                }else {                    setAfterData();                    Toast.makeText(SecondActivity.this, "修改成功", Toast.LENGTH_SHORT).show();                }               }        });         }}

在第二个Activity我们使用getIntent()接收有第一个Activity发送的数据。
而反馈信息我们使用setResult(RESULT_OK, intent);
setResult(RESULT_OK, intent);使用有个重点,那就是这必须在finish()前执行。也就是说我们不能再onPause()、onStop()、onDestroy()中执行。

0 0
原创粉丝点击