0

大家好,我对此很陌生,可能需要一些帮助,这很简单,

我需要在此代码中添加什么

   public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv2 = (TextView) findViewById(R.id.textView2);
    Button b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               TextView tx = (TextView) findViewById(R.id.textView2);
               tx.setText("Hello");
           }
    });
    }
    }

为了做到这一点,当按下按钮时,如果我标记了字符串、string1、string2 等,它会将文本视图更改为我的 strings.xml 列表中的随机字符串。

4

2 回答 2

1

您为什么不创建string-array并使用它,如下所示:

试试这种方式:

Resources res = this.getResources();
  final String[] capital = res.getStringArray(R.array.list);
  Button next=(Button)findViewById(R.id.button1);
   button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(count<capital.length-1)
        tv2.setText(capital[count]);
        count++;
    }
});
于 2013-01-06T15:54:28.957 回答
0
public class main extends Activity {
        /** Called when the activity is first created. */

        String[] myStrings;
        int currentText;
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv2 = (TextView) findViewById(R.id.textView2);
        Button b = (Button) findViewById(R.id.button1);
        currentText = 0;
        setListeners();

        myStrings = new String [200];
        myStrings[0] = "String1";
        myStrings[1] = "String2";
    }


    public void changeText(){
        tv2.setText(myStrings[currentText[);
        currentText++;

    }

    public void setListeners(){
        b = (Button) findViewById(R.id.textView2);
                b.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        changeText();

                        return false;
                    }
                });
    }
}

我没有测试这段代码。我希望这可以帮助你!

于 2013-01-06T15:26:30.643 回答