android学习日记(Activity初步)

小技巧:

1.右键 ——>source——>Override/Implement Methods ——>onCreate——>ok (自动生成代码框架)
2.eclipse自动联想快捷键(alt+/)



Intent 请求



1.Component name  启动哪个activity

2.Action  activity做什么动作

3.Data  传送什么数据

4.Category

5.Extras 传递键值对

6.Flags     

        

***每个activity 都需要在AndroidManifest.xml文件中注册***


功能 :点击按钮 从activity03跳转到otheractivity 把activity03的字符串通过Intent的Extras传递到otheractivity 中并显示



       


MainActivity .java

package com.example.activity03;

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

public class MainActivity extends Activity {

	private Button mybutton=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mybutton=(Button)findViewById(R.id.mybutton);	
		//把下面的监听器的对象捆绑到按钮上
		mybutton.setOnClickListener(new mybuttonlistener());
		
	}
	class mybuttonlistener  implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//生成一个Intent对象
			Intent intent = new Intent();
			intent.putExtra("tempstr", "so great!");
			//从哪个activity(当前类的对象)跳转到哪个activity(请求谁)
			intent.setClass(MainActivity.this,otheractivity.class);
			MainActivity.this.startActivity(intent);	
		}
		
	}

	@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;
	}

}

otheractivity.java

package com.example.activity03;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;  
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);
		Intent intent=getIntent();
		String str=intent.getStringExtra("tempstr");
		
		
		mytextview=(TextView)findViewById(R.id.mytextview);
		mytextview.setText(str);
	}
	
}

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" >

  
    <Button
        android:id="@+id/mybutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

other.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/mytextview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
        
</RelativeLayout>

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

</manifest>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值