Viewing Text view
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 TextView
s 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.
精彩评论