• Android--Intent的使用


    1、Activity之间的切换通过Intent来完成。

      1)清单文件,配置好Activity,所有Activity都需要在该文件中配置。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.luxh.intent"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="11"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <!-- 第一个Activity -->
            <activity
                android:name="cn.luxh.intent.FirstActivity"
                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 -->
            <activity
                    android:name="cn.luxh.intent.SecondActivity"
                    android:label="@string/second_activity">
                    <intent-filter>
                           <action android:name="cn.luxh.intent.SecondActivity"/> <!--意图筛选器名称 -->
                           <category android:name="android.intent.category.DEFAULT"/><!--意图筛选器类别 -->
                    </intent-filter>
            </activity>
        </application>
    
    </manifest>

      2)Activity代码

    package cn.luxh.intent;
    
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.app.Activity;
    import android.content.Intent;
    
    public class FirstActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
            handleBtnJumpClick();
        }
        
        /**
         * 处理按钮点击事件
         */
        private void handleBtnJumpClick() {
            Button btn_jump = (Button) findViewById(R.id.btn_jump);
            btn_jump.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //创建一个Intent
                    
                    /*Intent intent = new Intent();
                    intent.setClass(FirstActivity.this, SecondActivity.class);*/
                    
                    //通过意图筛选器名称创建Intent
                    Intent intent = new Intent("cn.luxh.intent.SecondActivity");
                    
                    //启动新的Activity
                    startActivity(intent);
                }
            });
        }
    
    }
    package cn.luxh.intent;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class SecondActivity extends Activity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
        }
    }

       3)运行效果

    2、从Intent中返回数据

      在第二个Activity中输入用户名,然后在第一个Activity中显示。

      1)布局文件activity_second.xml

    <?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" >
        
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/second_activity_textview"
                android:layout_gravity="center_horizontal"/>
            
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/second_activity_textview_username"/>
            
            <EditText 
                android:id="@+id/edit_text_username"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="text"/>
            
            <Button 
                android:id="@+id/btn_submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/second_activity_btn_submit"
                android:layout_gravity="right"/>
    </LinearLayout>

      2)Activity代码

    package cn.luxh.intent;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    import android.app.Activity;
    import android.content.Intent;
    
    public class FirstActivity extends Activity {
        
        public static final int REQUEST_CODE = 1;//标识代码
        
        private static final String TAG = "FirstActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
            handleBtnJumpClick();
        }
        
        /**
         * 处理按钮点击事件
         */
        private void handleBtnJumpClick() {
            Button btn_jump = (Button) findViewById(R.id.btn_jump);
            btn_jump.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //创建一个Intent
                    
                    /*Intent intent = new Intent();
                    intent.setClass(FirstActivity.this, SecondActivity.class);*/
                    
                    //通过意图筛选器名称创建Intent
                    Intent intent = new Intent("cn.luxh.intent.SecondActivity");
                    
                    //启动新的Activity,新的Activity结束后有结果返回
                    startActivityForResult(intent, REQUEST_CODE);
                }
            });
        }
        
        /**
         * startActivityForResult(intent, REQUEST_CODE)启动的新Activity结束调用该方法获取返回值
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode){
            case REQUEST_CODE:
                if(resultCode == RESULT_OK) {
                    String username = data.getData().toString();
                    Log.d(TAG, "返回的数据是:"+username);
                    Toast.makeText(this,username, Toast.LENGTH_LONG).show();
                }
            }
        }
    
    }
    package cn.luxh.intent;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class SecondActivity extends Activity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            handleBtnSubmitClick();
        }
        
        /**
         * 处理提交按钮点击
         */
        private void handleBtnSubmitClick(){
            Button btn_submit = (Button) findViewById(R.id.btn_submit);
            btn_submit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //获取输入的用户名
                    EditText et_username = (EditText) findViewById(R.id.edit_text_username);
                    String username = et_username.getText().toString();
                    Intent data = new Intent();
                    data.setData(Uri.parse(username));
                    setResult(RESULT_OK, data);//RESULT_OK = -1, operation succeeded
                    finish();//关闭当前Activity
                }
            });
        }
    }

      3)运行效果

  • 相关阅读:
    tomcat-1
    oscache-2
    oscache-3
    oscache-1
    oscache-4
    缓存概要
    Criterion & DetachedCriteria
    Hibernate <查询缓存>
    Hibernate <二级缓存>
    Hibernate <一级缓存>
  • 原文地址:https://www.cnblogs.com/luxh/p/3471115.html
Copyright © 2020-2023  润新知