2012-06-11 16 views
0

尋找一個控件,允許一次選擇一個文本值,每邊有兩個箭頭的android。不知道什麼控制的調用,但這裏有一個例子形象:在android中尋找一個選項旋鈕/選項輪控制器

enter image description here

+0

我從來沒有見過這樣的一個控制。你可能只需要自己創建它。 – Barak

+0

我看到它在很多地方,但不認爲我已經看到它在Android。如果我沒有得到答案,我會自己做。如果我沒有,我只是想避免重新發明輪子(抱歉)。 – Zammbi

+0

我應該用「for android」限定我的答案。我也在其他地方看到過。 :) – Barak

回答

0

創建我自己的,這對我很好。 Galley控制幫助了很多。儘管指出任何問題。

import android.content.Context; 
import android.graphics.Color; 
import android.util.AttributeSet; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.*; 
import nz.co.nuffie.android.crichq.R; 

import java.util.ArrayList; 
import java.util.List; 

public class ScrollWheelControl extends LinearLayout{ 
    private final ArrayList<String> textList = new ArrayList<String>(); 
    private ArrayAdapter optionAdapter = null; 
    private AdapterView.OnItemSelectedListener itemSelectedListener = null; 

    public ScrollWheelControl(Context context){ 
     super(context); 
    } 

    public ScrollWheelControl(Context context, AttributeSet attrs){ 
     super(context, attrs); 
     initView(context); 
    } 

    public ScrollWheelControl(Context context, AttributeSet attrs, int defStyle){ 
     super(context, attrs, defStyle); 
     initView(context); 
    } 

    public void initView(Context context) { 
     View.inflate(context, R.layout.scroll_wheel_control, this); 

     optionAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_gallery_item, textList) 
     { 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View v = super.getView(position,convertView,parent); 
       if(v instanceof TextView){//Just in case. 
        TextView txt = (TextView)v; 
        Gallery.LayoutParams glp = new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT); 
        txt.setGravity(Gravity.CENTER); 
        txt.setLayoutParams(glp); 
       } 
       return v; 
      } 
     }; 


     optionAdapter.notifyDataSetChanged(); 

     final Gallery g = (Gallery) findViewById(R.id.gOptionSelect); 
     g.setAdapter(optionAdapter); 

     final Button btnLeft = (Button) findViewById(R.id.btnLeft); 
     final Button btnRight = (Button) findViewById(R.id.btnRight); 
     g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> list, View view, int position, long id) { 
       if(position >= textList.size()-1){ 
        btnRight.setTextColor(Color.GRAY); 
       }else 
        btnRight.setTextColor(Color.WHITE); 

       if(position == 0){ 
        btnLeft.setTextColor(Color.GRAY); 
       }else 
        btnLeft.setTextColor(Color.WHITE); 

       if(itemSelectedListener != null){ 
        itemSelectedListener.onItemSelected(list,view,position,id); 
       } 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
      } 
     }); 
     btnLeft.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View view){ 
       g.onFling(null, null, 2000, 0); 
      } 
     }); 
     btnRight.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View view){ 
       g.onFling(null, null, -2000, 0); 
      } 
     }); 
    } 

    public void setOnItemListener(AdapterView.OnItemSelectedListener setItemSelectedListener){ 
     itemSelectedListener = setItemSelectedListener; 
    } 

    public void addTextItems(List<String> list){ 
     textList.addAll(list); 
     if(optionAdapter != null){ 
      optionAdapter.notifyDataSetChanged(); 
     } 
    } 
} 

XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:padding="5dp"> 

    <Button 
     android:id="@+id/btnLeft" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="\u003C" 
     android:background="@android:color/transparent" 
     android:textColor="@android:color/white" 
     android:shadowColor="@android:color/black" 
     android:shadowDx="1" 
     android:shadowDy="1" 
     android:shadowRadius="2" 
     android:textStyle="bold" 
     android:minWidth="15dp" 
      /> 

    <Gallery 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:id="@+id/gOptionSelect" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center_vertical" 
     android:spacing="0dp"> 
    </Gallery> 

    <Button 
     android:id="@+id/btnRight" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" 
     android:text="\u003E" 
     android:textColor="@android:color/white" 
     android:shadowColor="@android:color/black" 
     android:shadowDx="1" 
     android:shadowDy="1" 
     android:shadowRadius="2" 
     android:textStyle="bold" 
     android:minWidth="15dp" 
      /> 

</LinearLayout> 
2

如果它是迄今爲止,我可能會建議你使用它看起來像this的DatePicker的。否則,如果是時間,那麼可能你可能需要像this這樣的東西。

但是,如果您正在尋找類似於NumberPicker的東西,它似乎只能使用API​​ 11及更高版本。因此,如果你想,你可能需要定製你自己的。

+0

沒有那些。它僅用於文本。 Android中沒有這樣的控制,但可能有一個庫/示例文本。我可能需要做出來。 – Zammbi

+1

嗯..還有一個微調這就像一個下拉列表,這使得每次一個選擇,但我想你應該讓你自己的,你給它更好看的示例圖像。 – 2012-06-11 04:33:36

+0

Ohya,如果你想,旋看起來是這樣的:HTTP://i41.tinypic.com/23l06dy.jpg – 2012-06-11 04:35:08

1

嘿,我想看看this車輪控制..由第三方人員創建的自定義控件。他還在他的博客中提供了其他細節..你可以修改它,如果你想..

+0

感謝您的回答。我確實看了一下,這是我想要的,但將其轉化爲橫向的工作很多。 – Zammbi

相關問題