实现安卓页面的跳转

       在实际使用APP的时候,我们会发现按下某个按钮后,画面会进行切换,这样就可以把不同数据分门别类的放置在不同的页面上,一是避免画面过于拥挤,二是让用户的界面更为简洁,对程序的维护也有相当大的帮助。此功能的设计非常实用,我们在大多数的APP中都会看到这样的设计。

StartActivity -- 换页

(1)新建一个Android应用程序,打开资源文件目录中的 res\layout\main.xml ,增加一个新的Button按钮。

<span style="font-size:12px;"><strong></strong></span><pre name="code" class="html"><span style="font-size:12px;"><strong><?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
<span style="white-space:pre">	</span>android:orientation="vertical"
<span style="white-space:pre">	</span>android:layout_width="fill_parent"
<span style="white-space:pre">	</span>android:layout_height="fill_parent"
>
<span style="white-space:pre">	</span><TextView
<span style="white-space:pre">	</span>android:layout_width="fill_parent"
<span style="white-space:pre">	</span>android:layout_height="wrap_content"
<span style="white-space:pre">	</span>android:text="page2"
<span style="white-space:pre">	</span>/>
<span style="white-space:pre">	</span></strong></span>
<span style="font-size:12px;"><strong><span style="white-space:pre">	</span></LinearLayout></strong></span>
<Button android:text="Button" android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button></LinearLayout>
 

其中第12-17行:<Button>....</Button>加入一个Button按钮的UI元素。

第14行:android:id="@+id/button1"定义Button的控件句柄为Button1。

(2)新建第二个页面并打开 page2layout.xml 进行修改,这样就设计出第二个页面,XML文件内容如下

记得新建一个新的名为page2的Javaclass以便调用~

<span style="font-size:12px;"><strong><?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
<span style="white-space:pre">	</span>android:orientation="vertical"
<span style="white-space:pre">	</span>android:layout_width="fill_parent"
<span style="white-space:pre">	</span>android:layout_height="fill_parent"
>
<span style="white-space:pre">	</span><TextView
<span style="white-space:pre">	</span>android:layout_width="fill_parent"
<span style="white-space:pre">	</span>android:layout_height="wrap_content"
<span style="white-space:pre">	</span>android:text="@String/hello"
<span style="white-space:pre">	</span>/>

</LinearLayout></strong></span>

(3)修改源文件mainActivity.java

<span style="font-size:12px;"><strong>public class mainActivity extends Activity {

public void onCreate(Bundle saveInstanceState){
<span style="white-space:pre">	</span>super.onCreate(R.layout.main);
<span style="white-space:pre">	</span>Button b1 = (Button) findViewById(R.id.button1);
<span style="white-space:pre">	</span>b1.setOnClickListener(new Button.OnClickListener()
<span style="white-space:pre">	</span>public void onClick(View v){
<span style="white-space:pre">		</span>Intent intent = new Intent();
<span style="white-space:pre">		</span>intent.setClass(mainActivity.this,page2.class);
<span style="white-space:pre">		</span>startActivity(instent);
<span style="white-space:pre">		</span>mainActivity.this.finish();
<span style="white-space:pre">		</span>});
<span style="white-space:pre">	</span>}
}</strong></span>
这段代码中第15行:setContentView(R.layout.main);设定软件的显示界面为res\layout\main.xml。

1,Button b1 = (Button) findViewById(R.id.button1);是取得控件句柄的函数,作用是取得main.xml中android:id取值为button1 的Button组件。

2,b1.setOnClickListener(new Button.OnClickListener).setAdapter(new ImageAdapter(this));设置按钮的控制都由this来处理,this的功能是指向本身的类。

3,为了触发Button行为,我们使用 onClick 函数。

4,Intent intent = new Intent();初始化一个Intent类。

5,intent.setClass(mainActivity.this,paga2.class);准备进入下一页这一动作,并且指定page2.class为下一页的类;

6,startActivity(intent);切换页面!(重点)

7,mainActivity.this.finish();关闭当前页面。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值