【问题标题】:Displaying an array of values to buttons向按钮显示一组值
【发布时间】:2012-12-27 14:28:25
【问题描述】:

我有一个包含 12 个值的数组,我想一个接一个地显示它。最初在应用程序运行时,我显示 6 个值。在按钮上单击我想一一显示其他 6 个值。

prevbutton-> value1 value2 value3 value4 value5 value6 -> nextbutton

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

prevbutton-> value2 value3 value4 value5 value6 value7 -> nextbutton

这应该持续到 12 个值,并且在 prevbutton 的情况下应该发生相反的情况。

我用过这段代码,但它不起作用:

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.radionextstn:
        forwardtune();
        break;
    }
}

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]);
    }
}

我正在显示前 6 个值,如下所示:

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

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

@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 };
    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]);
    }
}

【问题讨论】:

标签: android arrays button


【解决方案1】:

我看到的第一个错误,您声明了一个包含 5 个按钮的数组并尝试放置 6 个按钮:

Button[] buttons = new Button[5];

你应该这样做:

Button[] buttons = new Button[6];

【讨论】:

  • 非常感谢,哦,你说得对。正如你所说,我改变了我的代码,但一点运气都没有
  • 你的数组大小不是 12 而是 13 :-)
  • 不要为 eack onClick 事件调用 findViewById(),而是为所有 6 个按钮创建一个实例变量。
【解决方案2】:

解决方案:

if(currentlyDisplayingFromPosition + 6 >= arraySize)
    return;

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

【讨论】:

    【解决方案3】:

    根据需要修改。

    Button[] btns = new Button[8];
    List<String> pages = new ArrayList<String>();
    int mStart = 1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_activiy);
        btns[0] = (Button) findViewById(R.id.pr);
        btns[1] = (Button) findViewById(R.id.b1);
        btns[2] = (Button) findViewById(R.id.b2);
        btns[3] = (Button) findViewById(R.id.b3);
        btns[4] = (Button) findViewById(R.id.b4);
        btns[5] = (Button) findViewById(R.id.b5);
        btns[6] = (Button) findViewById(R.id.b6);
        btns[7] = (Button) findViewById(R.id.nt);
    
        btns[0].setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                rearrangeBy(-1);
            }
        });
        btns[btns.length - 1].setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                rearrangeBy(1);
            }
        });
        // setup listeners
        for (int i = 1; i < btns.length - 1; i++) {
            btns[i].setOnClickListener(mClickListener);
        }
        for (int i = 0; i < 12; i++) {
            pages.add((i + 1) + "");
        }
        setUpButtons();
    }
    
    final View.OnClickListener mClickListener = new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            Toast.makeText(DialogActiviy.this, v.getTag() + "",
                    Toast.LENGTH_SHORT).show();
    
        }
    };
    
    private void rearrangeBy(int by) {
        mStart += by;
        setUpButtons();
    }
    
    private void setUpButtons() {
        // setup previous button
        btns[0].setEnabled(!(mStart == 1));
        // setup next button
        int end = pages.size() - btns.length + 2 + 1;
        btns[btns.length - 1].setEnabled(!(mStart == end));
        // setup intermediate buttons
        for (int i = 1; i < btns.length - 1; i++) {
            String text = (mStart + i - 1) + "";
            btns[i].setText(text);
            btns[i].setTag(text);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-25
      • 2017-08-03
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多