Android开发-Activity

下面为本次APK代码内容,具体实现功能为Activity之间跳转,传值/接收,启动和关闭
<span style="font-family: Arial, Helvetica, sans-serif;">代码模块:</span>
</pre><p></p><p><table border="1" width="200" cellspacing="1" cellpadding="1"><tbody><tr><th scope="col">编号</th><th scope="col">路径(模块)</th><th scope="col">文件名称</th></tr></tbody><tbody><tr><td>1</td><td>src</td><td>MainActivity.java</td></tr><tr><td>2</td><td>src</td><td>Aty1.java</td></tr><tr><td>3</td><td>res-layout</td><td>activity_main.xml</td></tr><tr><td>4</td><td>res-layout</td><td>aty1.xml</td></tr><tr><td>5</td><td>文件配置清单</td><td>AndroidManifest.xml</td></tr></tbody></table>MainActivity.java</p><p></p><p></p><pre name="code" class="java">package com.example.myapplication;

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


public class MainActivity extends Activity {
	private Button btnStartAty1;//声明按钮
	private TextView tvOut;//声明文本显示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//关联布局
        
        tvOut = (TextView) findViewById(R.id.tvOut);//关联组件-文本显示
        btnStartAty1 = (Button) findViewById(R.id.btnStartAty1);//关联组件-按钮
        
        
        
        btnStartAty1.setOnClickListener(new View.OnClickListener() { //添加监听器
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent i = new Intent(MainActivity.this,Aty1.class);
//				i.putExtra("txt", "Hello Aty1");
							
				//传递数据
				Bundle data = new Bundle();//声明bundle类型数据
				data.putString("txt", "Hello 1 Aty1");//设定数据		
				i.putExtras(data);//传入数据
				
				startActivityForResult(i, 0);//接收传回数据
				
				
//				startActivity(i);//启动Activity
			}
		});
        
        System.out.println("新建--onCreate");
    }

    
    //新增返回数据接收方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// TODO Auto-generated method stub
    	String result = data.getStringExtra("result");//声明接收数据
    	tvOut.setText(result);//显示接收数据
    	
    	super.onActivityResult(requestCode, resultCode, data);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    protected void onStart() {
    	// TODO Auto-generated method stub
    	super.onStart();
    	System.out.println("启动--onStart");
    }
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	System.out.println("继续运行--onResume");
    }
    
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub
    	super.onPause();
    	System.out.println("暂停--onPause");
    }
    
    @Override
    protected void onStop() {
    	// TODO Auto-generated method stub
    	super.onStop();
    	System.out.println("停止--onStop");
    }
    
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	System.out.println("关闭--onDestroy");
    }
    
    @Override
    protected void onRestart() {
    	// TODO Auto-generated method stub
    	super.onRestart();
    	System.out.println("重新启动--onRestart");
    }
    
    
}


Aty1.java


package com.example.myapplication;

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 Aty1 extends Activity {
	
	private Button btnClose;//声明按钮
	private TextView tvOut;//声明文本显示
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty1);
		
		btnClose = (Button) findViewById(R.id.btnClose);
		btnClose.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {

				
				Intent i = new Intent();
				i.putExtra("result", "Hello MainActiviy");
				setResult(0,i);
				
				finish();
				
			}
		});
		
		
		tvOut = (TextView) findViewById(R.id.tvOut);
//		tvOut.setText(getIntent().getStringExtra("txt"));		
		
		Bundle data = getIntent().getExtras();
		String txt = data.getString("txt");
		
		tvOut.setText(txt);
	}
	
	
}

activity_main.xml


<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tvOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btnStartAty1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="50dp"
        android:text="Start_Aty1" />

</RelativeLayout>

aty1.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:id="@+id/tvOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aty1" />

    <Button
        android:id="@+id/btnClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭" />

</LinearLayout>

AndroidManifest.xml


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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myapplication.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="Aty1"></activity>
    </application>

</manifest>











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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值