开发者

Viewing Text view

开发者 https://www.devze.com 2023-02-21 00:54 出处:网络
I am having set of array string. and i want to display a string one by one on clicking the button. for example if i click the button at fist time it should display first string a开发者_开发技巧nd agai

I am having set of array string. and i want to display a string one by one on clicking the button. for example if i click the button at fist time it should display first string a开发者_开发技巧nd again if i click the button it should display second string and so on.I dont know how to do this. can anyone guide me how to do this issue..


Have your OnClickListener walk through your string array and set the next value in the array as the TextViews text on each click.


Your Code :

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ButtonDemo extends Activity{

Button btn1;
TextView tv;
static int cnt=0;
int len;
String[] mydata={"text1","text2","text3"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.btn);
    len=mydata.length;

    btn1=(Button)findViewById(R.id.Button01);
    tv=(TextView)findViewById(R.id.TextView11);

    btn1.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            if(cnt==len)
            {
                cnt=0;
            }

            tv.setText(mydata[cnt]);
            cnt++;

        }

    });

}

}


Button b = (Button) findViewById(R.id.btn1);
int i=0;
b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                textView.setText(arrayItem[i]);
    i++;
            }
        });

this is how it works.

0

精彩评论

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