Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 基於Android移動終端的微型餐飲管理系統的設計與實現2-側滑菜單

基於Android移動終端的微型餐飲管理系統的設計與實現2-側滑菜單

編輯:關於Android編程

今天我先來寫一下側滑菜單,如圖:

\

 

側滑菜單的實現方法有很多,網上也有完整的開源框架,現在如果用Android Studio開發的話,一開始選模板的時候就可以選擇一個帶側滑菜單的模板。

我就把我寫的代碼貼出來給大家好了

xml文件:

 




    

    

        

        
    

    

        

        
    


    

        

        
    

    

        

        
    

    

        

        
    

    

        

        
    

    

        

        
    

    

        

        
    

    



 

然後是java代碼,因為側滑菜單的主要代碼基本都寫在MainActivity裡,我就都貼出來了,啟動應用的時候就先啟動這個activity,這個小應用主要也是這個activity了,各個頁面基本都是以fragment的形式依附在這個activity裡的,所以一些設置私有雲的代碼還有讀寫上次退出時所在狀態的代碼也在這裡

 

package com.exapmle.pc.restaurantclient;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.exapmle.pc.restaurantclient.Bluetooth.PrinterConnectDialog;
import com.exapmle.pc.restaurantclient.Fragments.AddFragment;
import com.exapmle.pc.restaurantclient.Fragments.BluetoothConnectFragment;
import com.exapmle.pc.restaurantclient.Fragments.DataFragment;
import com.exapmle.pc.restaurantclient.Fragments.FoodListFragmet;
import com.exapmle.pc.restaurantclient.Fragments.OrderFragment;
import com.exapmle.pc.restaurantclient.Fragments.PrinterSettingFragment;
import com.exapmle.pc.restaurantclient.Fragments.SignUpFragment;
import com.gprinter.aidl.GpService;
import com.gprinter.io.GpDevice;
import com.gprinter.service.GpPrintService;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import cn.bmob.v3.Bmob;

/*
1.錄入菜品
1.1 從手機獲取照片
1.2 調用攝像頭
1.3 菜名 價格 單位 簡介 規格1 規格2

 2.點餐
 3.數據統計
  */
public class MainActivity extends Activity implements View.OnClickListener {
    private static final String strState = "state";
    private static final String strStateMain = "stateMain";

    private int stateMain = 1;
    private AddFragment addFragment;
    private OrderFragment orderFragment;
    private DataFragment dataFragment;
    private Fragment bluetoothFragment;
    private BluetoothConnectFragment printerListFragment;
    private SignUpFragment signUpFragment;
    private TextView tvAddFragment;
    private TextView tvOrderFragment;
    private TextView tvDataFragment;
    private TextView tvBluetoothFragment;
    private TextView tvPrinterListFragment;
    private TextView tvClose;
    private TextView tvSignUpFragment;
    private TextView btnFoodListFragment;
    private SlidingMenu menu;

    public static GpService mGpService= null;
    public static final String CONNECT_STATUS = "connect.status";
    private static final String TAG = "MainActivity";
    private PrinterServiceConnection conn = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG,"4444444444444444444");

        //讀取顯示界面狀態數據
        SharedPreferences sp = this.getSharedPreferences(strState, MODE_PRIVATE);
        if (sp.getInt(strStateMain, 0) != 0) {//如果這個key不存在的話返回的數值
            stateMain = sp.getInt(strStateMain, 1);
        } else {
            changeMainState(1);
        }

        // 初始化 Bmob SDK
        // 使用時請將第二個參數Application ID替換成你在Bmob服務器端創建的Application ID
        Bmob.initialize(this, "******************************");
        connection();
        init();
    }
    /*
    以下為bluetooth建立連接的內容
     */
    private void startService() {
        Intent i= new Intent(this, GpPrintService.class);
        startService(i);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private void connection() {
        conn = new PrinterServiceConnection();
        Intent intent = new Intent("com.gprinter.aidl.GpPrintService");
        bindService(intent, conn, Context.BIND_AUTO_CREATE); // bindService
    }
    class PrinterServiceConnection implements ServiceConnection {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("ServiceConnection", "onServiceDisconnected() called");
            mGpService = null;
        }
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mGpService = GpService.Stub.asInterface(service);
        }
    }

    private void init() {
        /*
        設置slidingmenu
         */
        // configure the SlidingMenu
         menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        // 設置觸摸屏幕的模式
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        //menu.setShadowDrawable(R.drawable.shadow);
        //menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu劃出時主頁面顯示的剩余寬度
        menu.setBehindWidth(400);//設置SlidingMenu菜單的寬度
        // 設置漸入漸出效果的值
        menu.setFadeDegree(0.35f);

        /**
         * SLIDING_WINDOW will include the Title/ActionBar in the content
         * section of the SlidingMenu, while SLIDING_CONTENT does not.
         */
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        //為側滑菜單設置布局
        menu.setMenu(R.layout.slidingmenu_left);
        //設置要使菜單滑動,觸碰屏幕的范圍
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        menu.setOnClickListener(this);
        /*
        設置slidingmenu裡的按鈕
        */
        tvAddFragment= (TextView) findViewById(R.id.btnAddFragment);
        tvAddFragment.setOnClickListener(this);
        tvOrderFragment= (TextView) findViewById(R.id.btnOrderFragment);
        tvOrderFragment.setOnClickListener(this);
        tvDataFragment= (TextView) findViewById(R.id.btnDataFragment);
        tvDataFragment.setOnClickListener(this);
        tvBluetoothFragment=(TextView) findViewById(R.id.btnBluetoothFragment);
        tvBluetoothFragment.setOnClickListener(this);
        tvPrinterListFragment= (TextView) findViewById(R.id.btnPrinterListFragment);
        tvPrinterListFragment.setOnClickListener(this);
        tvSignUpFragment= (TextView) findViewById(R.id.btnSignUpFragent);
        tvSignUpFragment.setOnClickListener(this);
        tvClose= (TextView) findViewById(R.id.btnClose);
        tvClose.setOnClickListener(this);

        btnFoodListFragment= (TextView) findViewById(R.id.btnFoodListFragment);
        btnFoodListFragment.setOnClickListener(this);
        setFragment();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnClean: {
                break;
            }
            case R.id.btnSave: {
                break;
            }
            case R.id.btnAddFragment:{
                changeMainState(1);
                setFragment();
                break;
            }
            case R.id.btnOrderFragment: {
                //layoutBottom2.setBackgroundColor(Color.RED);
                changeMainState(2);
                setFragment();
                break;
            }
            case R.id.btnDataFragment: {
                //layoutBottom2.setBackgroundColor(Color.RED);
                changeMainState(3);
                setFragment();
                break;
            }
            case R.id.btnPrinterListFragment: {
                //layoutBottom2.setBackgroundColor(Color.RED);
                changeMainState(4);
                setFragment();
                break;
            }
            case R.id.btnBluetoothFragment: {
                //layoutBottom2.setBackgroundColor(Color.RED);
                changeMainState(5);
                //setFragment();
                printerList();
                break;
            }
            case R.id.btnSignUpFragent: {
                //layoutBottom2.setBackgroundColor(Color.RED);
                changeMainState(6);
                setFragment();
                break;
            }
            case R.id.btnFoodListFragment: {
                changeMainState(7);
                setFragment();
                break;
            }
            case R.id.btnClose: {
                finish();
                break;
            }
            default:
                break;
        }
    }

    //轉跳到打印機列表
    private void printerList() {
        Log.d(TAG, "openPortConfigurationDialog ");
        Intent intent = new Intent(this,
                PrinterConnectDialog.class);
        boolean[] state = getConnectState();
        intent.putExtra(MainActivity.CONNECT_STATUS, state);
        this.startActivity(intent);
    }


    public boolean[] getConnectState() {
        boolean[] state = new boolean[GpPrintService.MAX_PRINTER_CNT];
        for (int i = 0; i < GpPrintService.MAX_PRINTER_CNT; i++) {
            state[i] = false;
        }
        for (int i = 0; i < GpPrintService.MAX_PRINTER_CNT; i++) {
            try {
                if (MainActivity.mGpService.getPrinterConnectStatus(i) == GpDevice.STATE_CONNECTED) {
                    state[i] = true;
                }
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return state;
    }
    private void changeMainState(int i) {
        stateMain = i;
        SharedPreferences sp = this.getSharedPreferences(strState, MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(strStateMain, stateMain);
        editor.commit();
        menu.showContent();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setFragment() {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        switch (stateMain) {
            case 1: {
                addFragment = new AddFragment();
                transaction.replace(R.id.layout_main, addFragment);
                changeMainState(1);
                break;
            }
            case 2: {
                orderFragment = new OrderFragment();
                transaction.replace(R.id.layout_main, orderFragment);
                changeMainState(2);
                break;
            }
            case 3: {
                dataFragment = new DataFragment();
                transaction.replace(R.id.layout_main, dataFragment);
                changeMainState(3);
                break;
            }
            case 4: {
                bluetoothFragment = new PrinterSettingFragment();
                transaction.replace(R.id.layout_main, bluetoothFragment);
                changeMainState(4);
                break;
            }
            case 5: {
                printerListFragment = new BluetoothConnectFragment();
                transaction.replace(R.id.layout_main, printerListFragment);
                changeMainState(5);
                break;
            }
            case 6: {
                signUpFragment = new SignUpFragment();
                transaction.replace(R.id.layout_main, signUpFragment);
                changeMainState(6);
                break;
            }
            case 7: {
                FoodListFragmet foodListFragmet = new FoodListFragmet();
                transaction.replace(R.id.layout_main, foodListFragmet);
                changeMainState(7);
                break;
            }
            default:
                break;
        }
        transaction.addToBackStack(null);
        transaction.commit();
    }

}

    


 

Bmob雲服務平台請看這裡:

http://www.bmob.cn/

 

順便把activuty_main.xml裡的代碼貼一下,裡面雖然沒有什麼東西,但是這個界面很重要,因為其它界面都是依附在它上面的

 


    

    


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved