Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

基于Android移动终端的微型餐饮管理系统的设计与实现2-侧滑菜单

$
0
0

上周发了《 基于Android移动终端的微型餐饮管理系统的设计与实现》第一章,虽然是个简介,但是居然上了首页推荐,还有朋友评论了。写了这么久CSDN,还是第一次有人评论我的文章,真是受宠若惊,长期以来,我一直都以为,我玩的是CSDN单机版。当然,这也说明,我过去写的东西都太烂了,不过这也算是我真实水平的反应吧,希望随着自身水平的提高,能写出更好的博文来,即使写不出,也算是自己成长的记录吧。


今天我先来写一下侧滑菜单,如图:




侧滑菜单的实现方法有很多,网上也有完整的开源框架,现在如果用Android Studio开发的话,一开始选模板的时候就可以选择一个带侧滑菜单的模板。


具体实现方法可以看这篇文章:

http://blog.csdn.net/lmj623565791/article/details/36677279


我就把我写的代码贴出来给大家好了


xml文件:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:text="@string/text_btnAddFragment"></LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@drawable/leaf2" />

        <TextView
            android:id="@+id/btnAddFragment"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            style="@style/style_text"
            android:text="@string/text_btnAddFragment" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@drawable/leaf2" />

        <TextView
            android:id="@+id/btnOrderFragment"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            style="@style/style_text"
            android:text="@string/text_btnOrderFragment" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@drawable/leaf2" />

        <TextView
            android:id="@+id/btnDataFragment"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            style="@style/style_text"
            android:text="@string/text_btnDataFragment" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@drawable/leaf2" />

        <TextView
            android:id="@+id/btnPrinterListFragment"
            style="@style/style_text"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:text="@string/text_btnPrinterListFragment" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@drawable/leaf2" />

        <TextView
            android:id="@+id/btnBluetoothFragment"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            style="@style/style_text"
            android:text="@string/text_btnBluetoothFragment" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@drawable/leaf2" />

        <TextView
            android:id="@+id/btnSignUpFragent"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            style="@style/style_text"
            android:text="@string/text_btnSignUpFragent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@drawable/leaf2" />

        <TextView
            android:id="@+id/btnFoodListFragment"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            style="@style/style_text"
            android:text="查看菜单" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@drawable/leaf2" />

        <TextView
            android:id="@+id/btnClose"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            style="@style/style_text"
            android:text="退出" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>


然后是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里的代码贴一下,里面虽然没有什么东西,但是这个界面很重要,因为其它界面都是依附在它上面的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:id="@+id/layout_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8.3"
        android:orientation="horizontal">

    </LinearLayout>

</LinearLayout>




作者:qq_15062527 发表于2016/10/23 20:21:40 原文链接
阅读:79 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>