android组件之Activity

用两种方法实现两个界面的切换,



第一种,更换布局文件:

在main布局文件中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第一页" />

    <Button
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一页" />

</LinearLayout>

在second布局文件中:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第二页" />
    
    <Button
        android:id="@+id/layout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一页" />

</LinearLayout>


Activity中代码编写:

package cn.sword.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FirstActivity extends Activity {
    
	Button btn1;
	Button btn2;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        
        btn1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				nextLayout();
			}

		});
    }
    public void upLayout(){
		setContentView(R.layout.main);
		btn1 = (Button) this.findViewById(R.id.layout1);
		btn1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				nextLayout();
			}
		});
    }

	public void nextLayout() {
		setContentView(R.layout.second);
		btn2 = (Button) this.findViewById(R.id.layout2);
    	btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				upLayout();
			}
		});
	}
    
    public void findViews(){
    	btn2 = //获取可编辑文本框的ID
    	btn1 = (Button) this.findViewById(R.id.layout1);//获取Button按钮的ID
    }
}


第二种方法,两个Activity进行替换,同样有两个布局文件,分别对应两个Activity:


布局main中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/first" 
        android:layout_gravity="center"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/first_btn"
        android:text="下一页"
        android:layout_gravity="center"/>
</LinearLayout>

在main布局对应的FirstActivity中的代码编写:


package cn.sword.activity;

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

public class FirstActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn1 = (Button) this.findViewById(R.id.first_btn);
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
				FirstActivity.this.startActivity(intent);
			}
		});
    }
}

secondlayout布局文件中:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/second"
        android:layout_gravity="center" />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/second_btn"
        android:text="上一页"
        android:layout_gravity="center"/>
    
</LinearLayout>

在secondlayout布局文件对应的SecondActivity中的代码编写:


package cn.sword.activity;

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

public class SecondActivity extends Activity {

	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.secondlayout);
		
		Button btn2 = (Button) this.findViewById(R.id.second_btn);
		btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SecondActivity.this,FirstActivity.class);
				SecondActivity.this.startActivity(intent);
			}
		});
		
	}

	
}



上面两步完成之后,要记得在AndroidManifest.xml文件配置SecondActivity:


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


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


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".FirstActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />


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


</manifest>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值