菜鸟学习日志4.Intent与Activity


作为一个Android端的应用,多个Activity当然是避免不了的,我们该如何在多个Activity中跳转呢,这里就用到了Intent

下面我用一个按钮的响应跳转为例



先新建一个工程,这里我命名为IntentTest

在布局文件main里添加一个Button控件

代码如下

main.xml


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
<Button  
    android:id="@+id/myButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"   
    /> 
 
</LinearLayout>


然后在主程序上设置按键的触发


完整代码为

MainActivity.java


package com.example.intenttest;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {
	
    private Button myButton = null; 
    
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        myButton = (Button)findViewById(R.id.myButton); 
        myButton.setText("启动开关"); 
        myButton.setOnClickListener(new MyButtonListener()); 
    } 
     
  
 
    class MyButtonListener implements OnClickListener{ 
 
        public void onClick(View v) { 
            // TODO Auto-generated method stub  
            //生成一Intent对象  
            Intent intent = new Intent(); 
            intent.setClass(MainActivity.this,otherActivity.class); 
            MainActivity.this.startActivity(intent); 
        } 
             
        } 
  
}


编写另外一个Activity


package com.example.intenttest;

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
 
 
public class otherActivity extends Activity{ 
    private TextView myTextView = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.other); 
        myTextView = (TextView)findViewById(R.id.myTextView); 
        myTextView.setText(R.string.other); 
    } 
     
 
} 


对otherActivity的布局设计


<?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:id="@+id/myTextView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout>


当编写一个新的Activity  需在AndroidManifest中进行注册:

位置在</activity>和</application>之间

代码如下


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intenttest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        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=".otherActivity" android:label="@string/other"/>
    </application><pre name="code" class="html">
</manifest>

 


以及在value文件夹下的string.xml添加


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">IntentTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="other">otherActivity</string>
</resources>

结果





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值