android从一个类跳转到另一个类的基本操作

个人对android比较有兴趣,所以自学android。在学习的过程中,主要有四个核心组建类 Activity, Service,Broadcast Receiver和ContentProvider。

首先就从 Activity开始。

第一个例子是 写一个 Activity '有按钮 调转到另一个Activity上。

第一步:配置布局文件

<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="match_parent"
       android:layout_height="wrap_content"
       />

</RelativeLayout>

在布局文件中 ,做一个Button按钮,并配置其相应的属性。

第二步 建立一个类 并且继承Activity

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.setText("myButton");
		myButton.setOnClickListener(new MyButtonListenter());//事件监听*******

	}
其中 这个类要继承 Activity  而且要重写父类onCreate方法,用来实例化组件 。setContentView展示在Activity上的视图界面,它调用的layout_main的布局文件。然后使用findViewById找到Button,实例化Button按钮,并且绑定在监听器上。监听器代码如下:
class MyButtonListenter implements View.OnClickListener {//*********
		@Override
		public void onClick(View v) {//*********
			// TODO Auto-generated method stub
			Intent intent = new Intent();//传给另一个OtherActivity值
			intent.putExtra("textIntent", "123");
			intent.setClass(MainActivity.this, OtherActivity.class);//跳转*******
			MainActivity.this.startActivity(intent);//*******
		}

	}

其中Intent是Activity跳转的纽带 他将值传递给另一个Activity。

第三步:每一个Activity都有对应的布局文件,所以对OtherActivity配置布局文件

<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=".OtherActivity" >

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

</RelativeLayout>


第四步:创建OtherActivity 同样继承Activity

public class OtherActivity extends Activity {
	private TextView myTextView = null;

	public void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.other);
		Intent intent = getIntent();
		String value = intent.getStringExtra("textIntent");
		myTextView = (TextView) findViewById(R.id.myTextView);
		myTextView.setText(value);
	}

}
其中的代码也和第一个Activity思想一样,

第四步:也是新手容易忽略的地方,就是在AndroidMainifest.xml中注册第二个Activity

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

    <uses-sdk
        android:minSdkVersion="8"
        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.activity02.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="com.activity02.OtherActivity"
            android:label="@string/other" >
        </activity>
    </application>

</manifest>
其中的
<intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter>
是选择Activity作为程序第一次运行时出现的Activity。

这样一个基本的用按钮跳转到另一个Activity的程序就写好了,其中的R.java文件是系统自行为程序创建的各种ID。不要修改。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值