【English】Android -> Training -> Building Your First App -> Starting Another Activity

Starting Another Activity

 

step 1 : create a xml for MainActivity ( activity_main.xml )

 

<LinearLayout 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:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click" />
    <Button 
        android:id="@+id/jump"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/forword"
        android:onClick="jump"
        />

</LinearLayout>

 

step 2 : create a xml for AnotherActivity ( activity_another.xml )

 

<LinearLayout 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:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/infomation" />

</LinearLayout>


 

step 3 : create a class (AnotherActivity.java)

 

package com.example.startanotheractivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class AnotherActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_another);
	}
	

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

}


step 4 : modify MainActivity.java

 

package com.example.startanotheractivity;

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

public class MainActivity extends Activity {

	private Button jump ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		jump = (Button) findViewById(R.id.jump);
	}
	
	public void jump(View view) {
		Intent jumpIntent = new Intent(this,AnotherActivity.class);
		startActivity(jumpIntent);
	}

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

}


notice : the Modifier of method jump that define in activity_another.xml  must be public

 

other resource : ( strings.xml )

 

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

    <string name="app_name">StartAnotherActivity</string>
    <string name="action_settings">Settings</string>
    <string name="click">Click on the button below, jump to the next Activity</string>
    <string name="forword">Jump</string>
    <string name="infomation">I am second Activity</string>

</resources>


 

ScreenShot

    --------------------->  



 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值