android小项目demo2

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="http://schemas.android.com/apk/res/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>

你可能感兴趣的:(android小项目demo2)