Android 台灣中文網

標題: Activity以觸控方式換頁 [打印本頁]

作者: 冠龍江    時間: 2013-3-21 14:34
標題: Activity以觸控方式換頁
本帖最後由 冠龍江 於 2013-3-21 14:45 編輯

各位大大們安安,小弟初心者一枚,希望跟大家一起成長~~

內容實在太長了,基本上都大同小異,所以只貼上部分程式碼

有興趣的可以下載附件
裡面包含打包好的APK,因為模擬器不支援觸控,所以想測試的同好,裝到手機裡面實機測試吧(支援Android 2.3以上)!!!

這是第一頁的程式碼:

package com.touchpage;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;

public class FirstActivity extends Activity {
       
        /*宣告*/
        private LinearLayout rightLayout;
        private LinearLayout leftLayout;
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
               
                /*呼叫自己設計的方法*/
                changelayout();
               
        }

        private void changelayout() {
               
                /*在layout設計我採用RelativeLayout(相對版面)做排版,
                 * 在這個版面中我又多加了兩個LineatLayout只為了控制,
                 * 當我觸控其中一個LinearLayout時會跳到下一個Activity*/

               
               
                /*找到要用的元件*/
                rightLayout = (LinearLayout)findViewById(R.id.rightLayout);
                /*註冊觸控的監聽器,並實作其方法*/
                rightLayout.setOnTouchListener(new rightTouchListener());
               
                /*找到要用的元件*/
                leftLayout = (LinearLayout)findViewById(R.id.leftLayout);
                /*註冊觸控的監聽器,並實作其方法*/
                leftLayout.setOnTouchListener(new leftTouchListener());
               
        }
       
        /*當觸控到右邊的LinearLayout時*/
        class rightTouchListener implements OnTouchListener{
                @Override
                public boolean onTouch(View v, MotionEvent event){
                        if(event.getAction() == MotionEvent.ACTION_UP){
                               
                                Intent intent = new Intent();
                                intent.setClass(FirstActivity.this , SecondActivity.class);
                                startActivity(intent);
                               
                                /*提醒視窗*/
                                Toast.makeText(FirstActivity.this,
                                                       R.string.second_page,
                                                       Toast.LENGTH_SHORT)
                                                       .show();
                               
                                /*關閉當前的Activity*/
                                FirstActivity.this.finish();
                               
                        }
                        return true;
                }
        }
       
        /*當觸控到左邊的LinearLayout時*/
        class leftTouchListener implements OnTouchListener{
                @Override
                public boolean onTouch(View v, MotionEvent event){
                        if(event.getAction() == MotionEvent.ACTION_UP){
                               
                                Intent intent = new Intent();
                                intent.setClass(FirstActivity.this , ThrActivity.class);
                                startActivity(intent);
                               
                                /*提醒視窗*/
                                Toast.makeText(FirstActivity.this,
                                                       R.string.thr_page,
                                                       Toast.LENGTH_SHORT)
                                                       .show();
                               
                                /*關閉當前的Activity*/
                                FirstActivity.this.finish();
                        }
                        return true;
                }
        }
       

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.first, menu);
                return true;
        }

}


第一頁的Layout設計:
firstlayout.jpg
登錄/註冊後可看大圖