博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android小项目demo2
阅读量:6166 次
发布时间:2019-06-21

本文共 7278 字,大约阅读时间需要 24 分钟。

hot3.png

MainActivity:

package cn.thewee.gourmetmeal.staff;

import java.util.Timer;

import java.util.TimerTask;

import android.app.AlertDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends BaseActivity {

 Button btn_order;

 Button btn_settleAccount;
 Button btn_orderRate;
 Button btn_increaseOrder;
 Button btn_more;
 
 String username;
 
 /**
  * 画廊
  */
 /**=================Gallery===============*/
  private Gallery pictureGallery = null ;
  private int[] picture = {
    R.drawable.gallery1,
    R.drawable.gallery2,
    R.drawable.gallery3,
    R.drawable.gallery4,
    R.drawable.gallery5,
    R.drawable.gallery6,
  };
  private int index = 0 ;
 /**=======================================*/
 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        username = getIntent().getStringExtra("username");
       
        //初始化各按钮Listener
        setListener();
    }
 /**
  * 初始化View变量
  */
 @Override
 protected void initViews() {
        setContentView(R.layout.main);
  
  pictureGallery = (Gallery)findViewById(R.id.gallery_main);
  ImageAdapter adapter = new ImageAdapter(this);
  this.pictureGallery.setAdapter(adapter);
  Timer timer = new Timer();
  timer.schedule(task, 2000, 2000);
  
  
  btn_order = (Button) this.findViewById(R.id.btn_order);
  btn_settleAccount = (Button) this.findViewById(R.id.btn_checkout);
  btn_orderRate = (Button) this.findViewById(R.id.btn_orderSchedule);
  btn_increaseOrder = (Button) this.findViewById(R.id.btn_increaseOrder);
  btn_more = (Button) this.findViewById(R.id.btn_more);
 }
 
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
     new AlertDialog.Builder(MainActivity.this)
     .setIcon(R.drawable.ic_launcher)
     .setTitle("提示")
     .setMessage("确认退出?")
        .setPositiveButton("确定",
                       new DialogInterface.OnClickListener(){
                               public void onClick(DialogInterface dialoginterface, int i){
                                System.exit(1);
                                }
                        })
        .setNeutralButton("返回",
                       new DialogInterface.OnClickListener(){
                               public void onClick(DialogInterface dialoginterface, int i){

                                }

                        })                        
        .show();
  return super.onKeyDown(KeyEvent.KEYCODE_BACK, event);
 } 
 /**
  * 定时器
  */
 private TimerTask task = new TimerTask() {
  
  @Override
  public void run() {
   Message message = new Message();
   message.what = 2 ;
   index = pictureGallery.getSelectedItemPosition();
   index ++ ;
   handler.sendMessage(message);
  }
 };
 /**
  * Handler
  */
 private Handler handler = new Handler(){

  @Override

  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   switch (msg.what) {
   case 2:
    pictureGallery.setSelection(index);
    break;

   default:

    break;
   }
  }
  
 };
 
 /**
  * 初始化各按钮Listener
  */
 private void setListener() {
  OnBtnClickListener btn_listener = new OnBtnClickListener();
  btn_order.setOnClickListener(btn_listener);
  btn_settleAccount.setOnClickListener(btn_listener);
  btn_orderRate.setOnClickListener(btn_listener);
  btn_increaseOrder.setOnClickListener(btn_listener);
  btn_more.setOnClickListener(btn_listener);
  
 }

 class OnBtnClickListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   Intent i_openAct = null;
   switch(v.getId()){
   case R.id.btn_order://点菜
    i_openAct = new Intent(MainActivity.this, DishesMenuActivity.class);
    i_openAct.putExtra("mode", MODE_SUBMIT_ORDER);
    break;
   case R.id.btn_checkout://结台
    i_openAct = new Intent(MainActivity.this, OrderListActivity.class);
    i_openAct.putExtra("mode", MODE_CHECKOUT);
    break;
   case R.id.btn_orderSchedule://订单进度
    i_openAct = new Intent(MainActivity.this, OrderListActivity.class);
    i_openAct.putExtra("mode", MODE_ORDER_SCHEDULE);
    break;
   case R.id.btn_increaseOrder://加单,先到OrderList中选择流水号,后转入“点菜”界面
    i_openAct = new Intent(MainActivity.this, OrderListActivity.class);
    i_openAct.putExtra("mode", MODE_ORDER_ADD);
    break;
   case R.id.btn_more://更多
    i_openAct = new Intent(MainActivity.this, MoreActivity.class);
    break;
   }
   if(i_openAct != null){
    i_openAct.putExtra("username", username);
    startActivity(i_openAct);
   }
  }
 }
 
 /**
  * Gallery适配器
  * @author Administrator
  *
  */
 class ImageAdapter extends BaseAdapter{
  private int GalleryItemBackground;
     private Context context ;
    public ImageAdapter(Context context ){
     this.context = context ;
     TypedArray typedArray = context.obtainStyledAttributes(R.styleable.Gallery);
     GalleryItemBackground = typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
    }
 
  public int getCount() {
   return Integer.MAX_VALUE;
  }

  

  public Object getItem(int position) {
   return position;
  }

  

  public long getItemId(int position) {
   return position;
  }

  

  public View getView(int position, View convertView, ViewGroup parent) {
   ImageView imageView = new ImageView(context);
   imageView.setImageResource(picture[position % picture.length]);
   imageView.setScaleType(ImageView.ScaleType.FIT_XY);
   imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
   imageView.setBackgroundResource(GalleryItemBackground);
   return imageView;
  }
  
 }

 @Override

 protected void getExtras() {
  // TODO Auto-generated method stub
  
 }
}

main:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/menu_background"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingLeft="25dp"
    android:paddingRight="25dp"
    android:paddingTop="25dp" >

    <Gallery

        android:id="@+id/gallery_main"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:gravity="center" >

        <Button

            android:id="@+id/btn_order"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/btn_choosedish" />

        <Button

            android:id="@+id/btn_checkout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/btn_checkout" />

        <Button

            android:id="@+id/btn_orderSchedule"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/btn_order_schedule" />

        <Button

            android:id="@+id/btn_increaseOrder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/btn_add_order" />

        <Button

            android:id="@+id/btn_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/btn_more" />
    </LinearLayout>

    <!--

    <Button
        android:id="@+id/btn_browseOrder"
        android:text="查  单"
        android:layout_margin="5dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_changeTable"
        android:text="换  台"
        android:layout_margin="5dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_mergeTable"
        android:text="并  台"
        android:layout_margin="5dp"
        android:layout_weight="1"/>

    -->

</LinearLayout>

转载于:https://my.oschina.net/u/1994482/blog/412305

你可能感兴趣的文章
安装rrdtool出现的错误
查看>>
木马隐藏地点全搜查
查看>>
来自CES 2018的5G信号:5G手机今年可能还用不上
查看>>
Subversion版本控制
查看>>
奇怪的打印纸盘故障
查看>>
hyperledger v1.0.5 区块链运维入门(一)
查看>>
Mybatis-mapper-xml-基础
查看>>
5. GC 调优(基础篇) - GC参考手册
查看>>
Windows 7 XP 模式颜色质量只有16位的解决
查看>>
SonicWall如何安全模式升级防火墙
查看>>
Linux IPC实践(3) --具名FIFO
查看>>
从Atlas到Microsoft ASP.NET AJAX(6) - Networking, Application Services
查看>>
成长之路---写好一个类
查看>>
读取 java.nio.ByteBuffer 中的字符串(String) 写入方式flash.utils.ByteArray.writeUTF
查看>>
范围管理和范围蔓延
查看>>
android90 bind方式启动服务service调用service里的方法
查看>>
前端开发薪资之各地区对比(图文分析)(share)
查看>>
对做“互联网产品”的一些想法
查看>>
SPI协议及其工作原理浅析【转】
查看>>
原生js编写的安全色拾色器
查看>>