Android学习笔记02

Activity与Intent的案例


实现的效果:点击按钮跳转到另一个Activity


首先把默认的布局中的TextView改成Button

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

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

</RelativeLayout>


在Activity上添加Button事件

package fengda.activity02;

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

public class Activity02 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity02);
        Button button = (Button) findViewById(R.id.myButton);
        button.setText("点我,我就跳!!");
        button.setOnClickListener( new MyButtonListener());
    }
    
    class MyButtonListener implements OnClickListener {

    	public void onClick(View v) {
    		//建立一个空的Intent,用来传递数据
    		Intent myIntent = new Intent();
    		myIntent.setClass(Activity02.this, Activity02_1.class);
    		myIntent.putExtra("sendValue", "我是被传过来的!!!");
    		Activity02.this.startActivity(myIntent);
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_activity02, menu);
        return true;
    }
}



再添加跳转的Activity

package fengda.activity02;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Activity02_1 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity02_1);
        Intent myIntent = getIntent();
        TextView myTextView = (TextView) findViewById(R.id.myTextView);
        myTextView.setText(myIntent.getStringExtra("sendValue"));
    }
	
	
}


Activity02的布局文件

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

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>



在AndroidManifest.xml上注册

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="fengda.activity02"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Activity02"
            android:label="@string/title_activity_activity02" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity02_1"
            android:label="@string/title_activity_activity02_1" >
        </activity>
    </application>

</manifest>





效果调出来了

需要注意的问题在XML中最好不要直接使用中文,如果Activity label中有中文,会导致无法生成标


还在在Win7上使用Eclipse20120920-0800感觉好卡


调了好长时间没有解决一个问题,实在找不到怎么解决了

一直只提示这个错误,虽然不影响程序运行,但却怎么也找不到哪里出的问题

11-01 18:32:22.511: E/Trace(817): error opening trace file: No such file or directory (2)


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值