<Android>两个布局之间的跳转

Android布局显示,前端使用xml进行展示,后端使用Java代码进行控制 ,使用的类主要继承自Activity。

     创建一个布局文件进行的动作有:

     1.创建位于src包的.java文件

     2.创建位于res/layout中的.xml文件

     3.修改位于values中的strings.xml,添加对应的string

     4.修改AndroidManifest.xml中的activity内容


例如:创建一个MainActivity布局,则在项目中进行的动作有:

     1.src中生成MainActivity.class

     2.res/layout中生成activity_main.xml

     3.res/values的strings.xml中增加条目,默认增加

     <string name="app_name">MyUI</string>

     4.AndroidManifest.xml中增加条目

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>


 代码如下:mainactivity.java

package com.example.myui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("MainActivity", "OnCreate");		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		iv_time = (ImageView)findViewById(R.id.time);
		iv_time.setOnClickListener(new ivTimeListener());
		
	}	
		
	class ivTimeListener implements OnClickListener{
		@Override
		public void onClick(View v){
			Intent intent_ivtime = new Intent();
			intent_ivtime.setClass(MainActivity.this, TimeActivity.class);
			MainActivity.this.startActivity(intent_ivtime);
		}
	}
	
}
xml部分代码如下:

    <LinearLayout
        android:id="@+id/mthirdLinear"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/gray"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/time"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/gray"
            android:src="@drawable/time" >
        </ImageView>
    </LinearLayout>


创建的TimeActivity.java代码如下:
package com.example.myui;
import android.app.Activity;
import android.os.Bundle;
public class TimeActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_time);
	}
}
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="com.example.myui.TimeActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/time_detect"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="80sp" />
</RelativeLayout>

主要的跳转为:

                        Intent intent_ivtime = new Intent();
			intent_ivtime.setClass(MainActivity.this, TimeActivity.class);
			MainActivity.this.startActivity(intent_ivtime);

开启另一个activity的生命周期

     

     

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值