多種方式實現Android頁面布局的切換
用三種方式實現:按鈕切換,按鍵切換和觸摸切換。
先說我做的第一種方式邏輯:
先上代碼:
OneActivity.java文件代碼:
package cn.class3g; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class OneActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button nextButton = (Button)findViewById(R.id.next); nextButton.setOnClickListener(new OnClickListener() { @Override publicvoid onClick(View v) { setContentView(R.layout.two); ButtonupButton=(Button)findViewById(R.id.up); upButton.setOnClickListener(newOnClickListener() { @Override publicvoid onClick(View v) { setContentView(R.layout.main); ButtonnextButton = (Button)findViewById(R.id.next); } }); } }); } }
解釋:這是我最初寫的代碼,布局文件寫了兩個:main.xml和two.xml,分別顯示兩個頁面,分別有一個<TextView>和<Button>元素,具體代碼如下:
Main.xml代碼:
<?xml version="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/next" android:text="下一頁"/> </LinearLayout>
Two.xml代碼:
<?xmlversion="1.0" encoding="utf-8"?> <LinearLayoutxmlns: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:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/up" android:text="上一頁" /> </LinearLayout>
兩個xml文件寫完后就是寫邏輯了,java代碼如開頭寫的那樣,我是這樣想的:
默認的onCreate方法初始化后,通過setContentView調用布局文件main。Xml這就是默認的布局,然后聲明了一個 Button組件命名為nextButton,調用setOnClickListener監聽器實現對按鈕的監聽,在監聽器中new了一個點擊監聽器 onclickListener,在監聽器中實現了一個名為onClick的方法,在這里我寫了一個跳轉頁面的setContentView方法,通過點擊Button按鈕實現跳轉到two.xml布局頁面上,這樣就打開了two.xml頁面;在這個頁面上寫了另一個Button按鈕命名為:upButton用來返回上頁面,就這樣通過嵌套就實現了通過點擊按鈕實現頁面切換的效果。
但這不是我想要的效果,我要的效果是無論點擊哪個按鈕都會出現不同的樣式頁面,所以我通過對按鈕進行判斷來實現效果。首先想到的是用if語句進行判斷點擊的是哪個按鈕,然后進行不同的操作。我想這樣好點吧,但事實上不需要那么復雜去判斷鍵值就可以實現:
packagecn.class3g; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Button; public classActivityTest extends Activity { /** Called when the activity is firstcreated. */ @Override public void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button nextButton =(Button)findViewById(R.id.next); nextLayout();//顯示下一個頁面 } public void nextLayout(){ setContentView(R.layout.two); ButtonupButton=(Button)findViewById(R.id.up); upButton.setOnClickListener(newView.OnClickListener() { @Override public voidonClick(View v) { upLayout(); } }); } public void upLayout(){ setContentView(R.layout.main); ButtonnextButton = (Button)findViewById(R.id.next); nextButton.setOnClickListener(newView.OnClickListener() { @Override public voidonClick(View v) { nextLayout();//顯示下一個桌面 } }); } }
然后是通過按鍵實現頁面切換,通過老師上午的實例可以知道,在調用onClickDown或onClickUp的方法中調用頁面布局文件就可以實現:setContentView(R.layout.main);,在OnClickDown和onClickUp中分別添加兩個頁面的布局文件,再添加相反的方法,實現返回即可,代碼如下:
public boolean onKeyDown(int keyCode,KeyEvent event) { this.setContentView(R.layout.main); returnsuper.onKeyDown(keyCode, event); } public boolean onKeyUp(int keyCode,KeyEvent event){ //showInfo("keyUp"+keyCode); this.setContentView(R.layout.second); return super.onKeyUp(keyCode,event); }
觸摸實現換頁
public booleanonTouchEvent(MotionEvent event){ //showInfo("ontouch:x=" +event.getX() + " y="+event.getY()); flag = !flag; if(flag){ this.setContentView(R.layout.main); }else{ this.setContentView(R.layout.second); returnsuper.onTouchEvent(event); }
總結:
調用布局文件:setContentView(R.layout.main);
獲取組件ID:ButtonnextButton = (Button)findViewById(R.id.next);
做題的過程中重命名了一次java文件,導致無法調試,原因是:忘記更改AndroidManifest.xml文件中的<activity>下的<android:name>屬性值。