0

我想使用代码设置我的滚动视图元素。这是我到目前为止所拥有的(注释代码不起作用):

public class Dictionary extends Activity implements Runnable, OnClickListener {
    //Candidates
    candidatesScrollView = (HorizontalScrollView) findViewById(R.id.activity_dictionary_horizontalScrollView1);

    candidatesButtons = new ArrayList<Button>();
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button1));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button2));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button3));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button4));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button5));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button6));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button7));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button8));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button9));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button10));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button11));
    candidatesButtons.add((Button) findViewById(R.id.activity_dictionary_horizontalscrollview1_button12));
    for(int i = 0; i < candidatesButtons.size(); i++)
        ((Button) candidatesButtons.get(i)).setOnClickListener(this);

    (...)
}

然后在另一种方法中:

private void recognize() {

    (...)

    byte buffer[];
    buffer = RecogEngine.setResult(0); // and set the remaining data to engine
    try {
        candidatesString = new String(buffer, "UTF-8");     


    for (int i = 0; i < candidatesString.length(); i++)
    {
        candidatesButtons.get(i).setTextColor(getResources().getColor(R.color.Celadon));
        candidatesButtons.get(i).setTypeface(null,Typeface.BOLD);
        candidatesButtons.get(i).setText(String.valueOf(candidatesString.charAt(i)));
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    //HERE_I_WANT_TO_SELECT_AND_HIGHLIGHT (like in default HOLO_DARK THEME)
    //candidatesButtons.get(0).requestFocus();
    //candidatesButtons.get(0).setSelected(true);
    (...)
}

这个怎么做?

4

1 回答 1

0

我找到了解决我的问题的方法。您应该使用以下代码来设置选中的按钮:

//HERE THE CODE IS WORKING
candidatesButtons.get(0).requestFocusFromTouch();
candidatesButtons.get(0).setSelected(true);

也许它对某人有用

于 2013-06-13T04:27:07.177 回答