zoukankan      html  css  js  c++  java
  • 滚轮控件的定义和使用

    package com.ct.mywheel;
    
    import wheel.OnWheelChangedListener;
    import wheel.OnWheelScrollListener;
    import wheel.WheelView;
    import wheeladapter.AbstractWheelTextAdapter;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        private LinearLayout categaryLayout;
        private ImageView categaryImg;
        private TextView categaryTv;
        private String[] expense_explanation;
        private Integer[] expense_images;
        private Integer expenses_id=0,account_id=0;
        private boolean scrolling = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main); 
            categaryLayout = (LinearLayout)findViewById(R.id.expense_category);
            categaryImg = (ImageView)findViewById(R.id.expense_category_imv);
            categaryTv = (TextView)findViewById(R.id.expense_category_tv); 
            expense_explanation = getResources().getStringArray(R.array.EXPENSES_CATEGORY);
            expense_images = new Integer[]{R.drawable.repast, R.drawable.xiuxianyule,
                    R.drawable.book, R.drawable.shsj, R.drawable.juhuijiaoyou,
                    R.drawable.liwu, R.drawable.huafeiwangfei, R.drawable.transport,
                    R.drawable.cloth, R.drawable.touzikuisun, R.drawable.yiyao,
                    R.drawable.other};
            categaryLayout.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("选择支出种类");
                    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                    View view = inflater.inflate(R.layout.expense_category_wheel, null);
                    final WheelView expenses_category_wheel = (WheelView) view
                            .findViewById(R.id.expense_category_wheel);
                    expenses_category_wheel.setVisibility(3);
                    expenses_category_wheel.setViewAdapter(new MyExpensesAdapter(MainActivity.this,
                            expense_explanation, expense_images));
                    
                    expenses_category_wheel.addChangingListener(new OnWheelChangedListener() {
                        
                        @Override
                        public void onChanged(WheelView wheel, int oldValue, int newValue) {
                            // TODO Auto-generated method stub
                            expenses_id = newValue;
                        }
                    });
                    
                    expenses_category_wheel.addScrollingListener(new OnWheelScrollListener() {
                        
                        @Override
                        public void onScrollingStarted(WheelView wheel) {
                            // TODO Auto-generated method stub
                            scrolling = true;
                        }
                        
                        @Override
                        public void onScrollingFinished(WheelView wheel) {
                            // TODO Auto-generated method stub
                             scrolling = false;
                             expenses_id=expenses_category_wheel.getCurrentItem();
                        }
                    });
                    builder.setView(view);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            categaryImg.setImageResource(expense_images[expenses_id]);
                            categaryTv.setText(expense_explanation[expenses_id]);
                        }
                    }).setNegativeButton("取消", null).create().show();
                    
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
        private class MyExpensesAdapter extends AbstractWheelTextAdapter{
            
            private String[] explanation=null;
            private Integer[] images=null;
            
            
            
            @Override
            public View getItem(int index, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View view = super.getItem(index, convertView, parent);
                 ImageView img = (ImageView) view.findViewById(R.id.category);
                 img.setImageResource(images[index]);
                 TextView tv=(TextView) view.findViewById(R.id.expense_name);
                 tv.setText(explanation[index]);
                return super.getItem(index, convertView, parent);
            }
    
            public MyExpensesAdapter(Context context,String[] explanation,Integer[] images) {
                super(context,R.layout.expense_category_wheel_item);
                this.explanation=explanation;
                this.images=images;
            }
            
            protected MyExpensesAdapter(Context context) {
                super(context);
                // TODO Auto-generated constructor stub
            }
    
            @Override
            public int getItemsCount() {
                // TODO Auto-generated method stub
                return explanation.length;
            }
    
            @Override
            protected CharSequence getItemText(int index) {
                // TODO Auto-generated method stub
                return explanation[index];
            }
            
        }
        
        
    
    }

    (在F:\java\MyWheel)

  • 相关阅读:
    关于同余最短路
    【水】关于 __attribute__
    题解【AtCoder
    一些简单图论问题
    浅谈简单动态规划
    关于博客园主题(美化博客园)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第47章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第46章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第45章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第44章 读书笔记(待更新)
  • 原文地址:https://www.cnblogs.com/ct732003684/p/2921997.html
Copyright © 2011-2022 走看看