EditView实现左右选择

来源:互联网 发布:网络机房建设模板 编辑:程序博客网 时间:2024/05/09 20:49

共同学习,转载请注明出处!

本人不喜欢废话,直接上代码:先看效果图:

EditTextDemo.java

public class EditTextDemo extends Activity {
    private static final String TAG = "com.cn.liut.business.EditText";
    
    private EditText mModulationText;
    private EditText mModulationText_01;
    private static final String[] mModuItems = {"16QAM","32QAM","64QAM","128QAM","256QAM","QPSK"};
    private int moduItemsIndex = 1;
    private int getModuItemsLength(){
        return mModuItems.length;
    }
    /*
     * Toast的使用
     * Toast.makeText(EditTextDemo.this, "" + position, Toast.LENGTH_SHORT).show();
     */
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i(TAG, "onCreate======>>");
        mModulationText = (EditText)findViewById(R.id.search_item_param_modulation);
        mModulationText_01 = (EditText)findViewById(R.id.search_item_param_modulation_01);
        
        mModulationText.setText(mModuItems[moduItemsIndex]) ;
        mModulationText.setOnKeyListener(new EditText.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        if (moduItemsIndex <= 0) {
                            moduItemsIndex = getModuItemsLength();
                        }
                        moduItemsIndex--;
                        mModulationText.setText(mModuItems[moduItemsIndex]);
                        break;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        if (moduItemsIndex >= getModuItemsLength() - 1) {
                            moduItemsIndex = -1;
                        }
                        moduItemsIndex++;
                        mModulationText.setText(mModuItems[moduItemsIndex]);
                    default:
                        break;
                    }
                }
                return false;
            }
        });
        
        mModulationText_01.setText(mModuItems[moduItemsIndex]) ;
        mModulationText_01.setOnKeyListener(new EditText.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        if (moduItemsIndex <= 0) {
                            moduItemsIndex = getModuItemsLength();
                        }
                        moduItemsIndex--;
                        mModulationText_01.setText(mModuItems[moduItemsIndex]);
                        break;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        if (moduItemsIndex >= getModuItemsLength() - 1) {
                            moduItemsIndex = -1;
                        }
                        moduItemsIndex++;
                        mModulationText_01.setText(mModuItems[moduItemsIndex]);
                        break;
                    default:
                        break;
                    }
                }
                return false;
            }
        });
        
        
        /**
         * 触屏事件
         */
        mModulationText_01.setOnTouchListener(new EditText.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent motionEvent) {
                float x = motionEvent.getX();
                float y = motionEvent.getY();
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.i(TAG, "X : " + x + "===============y : " + y);
                    if(27.0 > x && x > 5.0 && y > 4.0 && y < 42.0) {
                        if (moduItemsIndex <= 0) {
                            moduItemsIndex = getModuItemsLength();
                        }
                        moduItemsIndex--;
                        mModulationText_01.setText(mModuItems[moduItemsIndex]);
                    }
                    if(316.0 > x && x > 300.0 && y > 4.0 && y < 42.0) {
                        if (moduItemsIndex >= getModuItemsLength() - 1) {
                            moduItemsIndex = -1;
                        }
                        moduItemsIndex++;
                        mModulationText_01.setText(mModuItems[moduItemsIndex]);
                    }
                }
                return false;
            }
        });
    }
}


mian.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    
    <EditText
        android:id = "@+id/search_item_param_modulation"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:drawableLeft="@drawable/search_modu_left_selector"
        android:drawableRight="@drawable/search_modu_right_selector"
        style="@style/et_style"
        android:editable="false"
        android:cursorVisible="false"
         />
    <EditText
        android:id = "@+id/search_item_param_modulation_01"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:drawableLeft="@drawable/search_modu_left_selector"
        android:drawableRight="@drawable/search_modu_right_selector"
        style="@style/et_style"
        android:editable="false"
        android:cursorVisible="false"
         />
</LinearLayout>

剩下的就是一些样式了,在代码中可以看到。代码下载:http://download.csdn.net/detail/lt01221/4283428


原创粉丝点击