开发者

How to refresh textview using onClick

In my Android program, I ha开发者_Python百科ve a textview that I want to refresh everytime a button is clicked. I have an array list of dates that I want to cycle through and display at the top of my UI. Any ideas?


Without knowing specifics of your activity, here is a very simple way of doing what you want. Don't forget to add some Date items to the array list.

 public class MyActivity extends Activity {
     int mIndex = 0;
     ArrayList<Date> dates;

     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.content_layout_id);

         dates = new ArrayList<Date>();

         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 TextView t = (TextView) findViewById(R.id.textview_id);
                 if (mIndex >= dates.size()) {
                     mIndex = 0;
                 }
                 if (dates.size() != 0) {
                     t.setText(dates.get(mIndex++).toLocaleString());
                 }
             }
         });
     }
 }


If I understand your question correctly, one possibility that doesn't require specifically handling the onClick for all of the items in your layout is to have a top-level RelativeLayout that contains your layout and a transparent surface that grabs focus and covers the screen, and then have all of its event handlers send the event on to the rest of the RelativeLayout contents and then update the TextView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜