小部件android中的Textview onclick()

时间:2013-07-03 08:02:48

标签: android onclick widget textview

我们在arraylist中存储了一些文本。我们希望在单个textview中显示它,当我单击textview时,应该在窗口小部件中更新arraylist中的下一个值(text)。

1 个答案:

答案 0 :(得分:0)

如果我理解你正在尝试做的事情,请试试这个:

public class MyActivity extends Activity {
    ArrayList<String> values = new ArrayList<String>();
    TextView myText;
    private int index;
    protected void onCreate(Bundle saved) {
         super.onCreate(saved);

         setContentView(R.layout.content_layout_id);
         index = 0;

         //example values:
         values.add("foo");
         values.add("bar");
         values.add("another value");

         myText = (TextView) findViewById(R.id.myTextId);
         //show the first value on myText
         myText.setText(values.get(index));

         myText.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Show next value in our text view:
                myText.setText(values.get(++index));
                //be sure to consider the ArrayList.size() so you won't try to present the 6th value in a 5 values ArrayList.
                //but this depends on your implementation.
             }
         });
     }
 }