【问题标题】:Android: display the array of values one after the other according to the button clickAndroid:根据按钮点击依次显示值数组
【发布时间】:2012-12-27 13:07:53
【问题描述】:

我有一个包含 12 个整数值的数组,我想一个接一个地显示它

最初在应用程序运行时,我显示 6 个值。

按钮点击我要一个一个显示其他6个值

prevbutton-> value1 value2 value3 value4 value5 value6 -> 下一个按钮

下一个按钮点击应该是这样的。

接下来的 6 个值应该像这样显示

prevbutton-> value2 value3 value4 value5 value6 value7 -> 下一个按钮

这应该持续到 12 个值

prevbutton 的情况下应该发生反转。

我们非常感谢您的帮助,谢谢

我正在显示像这样的前 6 个值

     public void autoTune() {
    Log.i("autotune", " called");

    if (frequency[0] == 0.0)
        station1.setText("");
    else
        station1.setText("" + frequency[0]);
    if (frequency[1] == 0.0)
        station2.setText("");
    else
        station2.setText("" + frequency[1]);
    if (frequency[2] == 0.0)
        station3.setText("");
    else
        station3.setText("" + frequency[2]);
    if (frequency[3] == 0.0)
        station4.setText("");
    else
        station4.setText("" + frequency[3]);
    if (frequency[4] == 0.0)
        station5.setText("");
    else
        station5.setText("" + frequency[4]);
    if (frequency[5] == 0.0)
        station6.setText("");
    else
        station6.setText("" + frequency[5]);

}



    @Override
    protected Boolean doInBackground(Context... params) {
        // TODO Auto-generated method stub
        Log.i("in autotune", " before freq length");
        int[] freq = { 911, 943, 947, 932, 901, 964, 843, 835, 946,904,908,873,877 };
        freqCounter = 0;
        Log.i("in autotune", "after freq length : " + freq.length);
        frequency = new double[freq.length];
        Log.i("in autotune", "after frequency length : " + frequency.length);
        for (int k = 0; k < freq.length; k++) {
            Log.i("In Radio.java", "Freq : " + freq[k]);
            frequency[k] = freq[k];
            frequency[k] = frequency[k] / 10;
            if (frequency[k] == 0.0)
                break;
            freqCounter++;
            Log.i("In Radio.java", "Frequency : " + frequency[k]);
        }

我已尝试使用此代码,但这根本不起作用

  public void forwardtune(){
Button[] buttons = new Button[5];
buttons[0] = (Button)findViewById(R.id.radiostation1);
buttons[1] = (Button)findViewById(R.id.radiostation2);
buttons[2] = (Button)findViewById(R.id.radiostation3);
buttons[3] = (Button)findViewById(R.id.radiostation4);
buttons[4] = (Button)findViewById(R.id.radiostation5);
buttons[5] = (Button)findViewById(R.id.radiostation6);
if(currentlyDisplayingFromPosition + 6 >= arraySize)
       return;

   for(int i=0; i<arraySize; i++){
       buttons[i].setText("");
   }
   for(int i=currentlyDisplayingFromPosition; i<=currentlyDisplayingFromPosition+6; i++){
       if (frequency[i] == 0.0)
           buttons[i].setText("");
        else
            buttons[i].setText("" + frequency[0]);
   }
  }

我在单击下一个按钮时调用此函数 forwardtune()。仍然没有运气!有什么建议吗?

【问题讨论】:

    标签: android arrays button onclick


    【解决方案1】:

    你可以试试这个。

    int currentlyDisplayingFromPosition = 0;
    int arraySize = 12;
    
    public void forwardTune(){
       if(currentlyDisplayingFromPosition + 6 >= arraySize)
           return;
    
       for(int i=0; i<arraySize; i++){
           textView[i].setText("");
       }
       for(int i=currentlyDisplayingFromPosition; i<=currentlyDisplayingFromPosition+6; i++){
           if (frequency[i] == 0.0)
                textView[i].setText("");
            else
                textView[i].setText("" + frequency[0]);
       }
    
    }
    

    同样你也可以通过反转上面的代码来尝试backwardTune()。

    【讨论】:

    • 非常感谢,但是什么是 textView[i]?我只有 6 个按钮,我在上面显示值。所以你想让我有一组按钮?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2015-07-10
    • 2021-06-01
    • 1970-01-01
    • 2011-02-01
    相关资源
    最近更新 更多