Android SDK 开发范例大全 ---3.8手机页面的转换

来源:互联网 发布:广州海度网络是培训吗 编辑:程序博客网 时间:2024/05/16 21:02

在网页的世界里,想要在两个网页间做转换,只要利用超链接(HyperLink)就可以实现,但在手机的世界,想要如何实现手机页面之间的转换呢?最简单的方法就是改变Activity的Layout!在这个范例里,将布局两个Layout,分别为Layout1(main.xml)与Layout2(mylayout.xml),默认加载的Layout为main.xml,且在Layout1中创建一个按钮,当单机按钮时,显示Layout2(mylayout,xml).同样地,在Layout2里也设计一个按钮,当单击第二个Layout的按钮之后,则显示回原来的Layout1.

src/com.helloworld/HelloWorldActivity.java

package com.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HelloworldActivity extends Activity {
    /** Called when the activity is first created. */
private Button mButton1,mButton2;
private TextView mTextView1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        mButton1 = (Button) findViewById(R.id.myButton1);
        mButton1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout2();
}        
        });
    }
public void jumpToLayout2() {
// TODO Auto-generated method stub
setContentView(R.layout.mylayout);
mButton2 = (Button) findViewById(R.id.myButton2);
mButton2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout1();
}
});
}
public void jumpToLayout1() {
// TODO Auto-generated method stub
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mButton1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout2();
}
});
}
}

/res/layout/main.xml

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


    <TextView
        android:id="@+id/myTextView1"    
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <Button
        android:id="@+id/myButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_1" />
     
</LinearLayout>

/res/layout/mylayout.xml

<?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:id="@+id/myTextView02"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/hidi"
       /> 


   <Button
        android:id="@+id/myButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_2" />
   
</LinearLayout>

0 0
原创粉丝点击