2013-07-19 53 views
0

我正在創建一個Android應用程序,我要添加一個水平滾動按鈕來增加/減少計數器。如何使用水平滾動按鈕增加或減少計數器

例如,如果用戶滾動向右按鈕時,計數器應增加1並向左滾動將由1

降低櫃檯請告訴我,我應該怎麼做才能完成任務。看看具有我想要的功能的附加圖像。

我想計數器增加,只有當用戶滾動特定的按鈕,而不是當他揮筆別處屏幕

enter image description here

+0

你能證明你的代碼?你用什麼作爲一個水平滾動按鈕?一個按鈕? SeekBar? – amatellanes

+0

@Adri我不知道我該怎麼走。這就是爲什麼我要求專業人士提供幫助,告訴他們做什麼纔是正確的方法。 –

+0

好吧,我會構建一段代碼,你可能會發現它有幫助。 – amatellanes

回答

0

。您可以使用一個SeekBar增加或減少的計數器。佈局的一個簡單的例子,你可以使用可能是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <SeekBar 
     android:id="@+id/seekBar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/counter" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="0" > 
    </TextView> 

</LinearLayout> 

而且竟被代碼是這樣的:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private TextView counter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Selects the TextView which holds the value of the counter 
     this.counter = (TextView) findViewById(R.id.counter); 

     SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar); 
     // Sets the initial value of the SeekBar (it must be the same initial 
     // value of the counter) 
     seekBar.setProgress(0); 
     // Sets the max value of the counter 
     seekBar.setMax(100); 
     seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, 
      boolean fromUser) { 
      // This code runs when you scroll the SeekBar to left or right. 
      // If you scroll to the left, the counter decreases and if you 
      // scroll to the right, the counter increases 
      counter.setText(String.valueOf(progress)); 
     } 
    }); 
} 

}

+0

非常感謝您花時間寫這段代碼。我目前在應用程序中有這個東西,但看起來有點無聊。所以我決定添加一些看起來不錯和有趣的東西,所以我會像水平滾動的東西(或按鈕或其他),使UI看起來不錯。例如,我們使用圓形水平滾動控件調整收音機。我想讓它變成這樣。 –