问题:在TextView上设置字符串数组

时间:2011-09-08 06:22:44

标签: android arrays

我想在单击下一个按钮时从textview更改文本。我试过这段代码却无法正确指导。你能解决它吗?

public class StringTestActivity extends ListActivity {
TextView t;
int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    t=(TextView)findViewById(R.id.text);

    Resources res = this.getResources();
    final String[] capital = res.getStringArray(R.array.Cap);

       //ArrayAdapter<String> n=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,capital);
       //setListAdapter(n);
    Button next=(Button)findViewById(R.id.btnNext);
    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(count<capital.length-1)
            t.setText(capital[count]);
            count++;
        }

    });

}  }

编辑:我遇到过此错误消息。 enter image description here

再次编辑: main.xml

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <TextView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:hint="text" />

   <Button android:text="Next" android:id="@+id/btnNext"
 android:layout_width="wrap_content" android:layout_height="wrap_content">   </Button>        

</LinearLayout>

RES /串/ array.xml

  <?xml version="1.0" encoding="utf-8"?>
 <resources>
  <string-array name="Cap">
    <item>A</item>
    <item>B</item>
    <item>C</item>
    <item>D</item>
  </string-array>
 </resources>

4 个答案:

答案 0 :(得分:2)

Button btn1;
 String countires[];
 int i=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.prob2);

    btn1 = (Button) findViewById(R.id.prob2_btn1);

    countires = getResources().getStringArray(R.array.country);

    for (String string : countires)
    {
        Log.i("--: VALUE :--","string = "+string);
    }

    btn1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            String  country  = countires[i];
            btn1.setText(country);
            i++;
            if(i==countires.length)
                i=0;
        }
    });
}

答案 1 :(得分:1)

@Override
        public void onClick(View v) {
            t.setText(capital[count]);
            count++;
            count=count%capital.length;
        }

答案 2 :(得分:1)

你的代码看起来不错,你的意思是“无法正确指导”?是否因异常而崩溃,代码是否永远不会改变?

顺便说一下,考虑到你的按钮被称为下一个,你可能想要像这样重写它:

    Override
    public void onClick(View v) {
        count++;
        if( count < capital.length ) // no - 1
        {
            t.setText(capital[count]);
        } else {
            /* perhaps v.setEnabled(false); ? */
        }
    }

答案 3 :(得分:1)

如果您未使用ListView,请实施活动而非 ListActivity 。所以进行此更改

public class StringTestActivity扩展了Activity {

导入android.app.Activity