如何调用新的字符串数组

时间:2016-02-13 04:00:55

标签: android arrays string

我有一个TextView,一个EditView和一个Submit按钮以及一个下一个按钮。在我的资源文件中,我有3个字符串数组。如果我在EditText中的答案是第一个字符串数组的正确答案,我想转到第二个字符串数组(下一个按钮)。再次,如果我在EditText中的答案是第二个字符串数组的正确答案,我想转到第三个字符串数组(下一个按钮),依此类推。以下是我的代码。怎么可能这样做。在person2之后,它返回到person1。这样做的更好方法是什么?有人可以提供一个例子。感谢。

//字符串阵列

<string-array name="person1">
    <item>He wore a black hat</item>
    <item>He's six feet tall</item>
    <item>He went to Harvard</item>
    <item>Moon</item>
</string-array>

<string-array name="person2">
    <item>She has a cooking show</item>
    <item>Her dogs name is Max</item>
    <item>She drives a red car</item>
</string-array>

<string-array name="person3">
    <item>He first appeared on a drama</item>
    <item>He's a friend of yours</item>
    <item>His hair is red</item>
</string-array>

// MainActivity

public class MainActivity extends AppCompatActivity {

    int mCounter= 0;
    int aCount = 0;
    int bCount = 0;
    TextView myTv;
    Button nextBtn;
    Button submitBtn;
    EditText msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nextBtn = (Button)findViewById(R.id.nextBtn);
        myTv = (TextView)findViewById(R.id.myTv);
        msg = (EditText)findViewById(R.id.msg);

        final String[]person1 = getResources().getStringArray(R.array.person1);
        final String[]person2 = getResources().getStringArray(R.array.person2);
        final String[]person3 = getResources().getStringArray(R.array.person3);
        nextBtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        myTv.setText(person1[mCounter]);

                        if (mCounter < person1.length -1){
                            mCounter++;
                        }else {
                            mCounter = 0;
                        }
                        if(msg.getText().toString().trim().toLowerCase().equals("MARK".toString().trim().toLowerCase())){

                            myTv.setText(person2[aCount]);

                            if (aCount < person2.length -1){
                                aCount++;
                            }else {
                                aCount = 0;
                            }
                            if (msg.getText().toString().trim().toLowerCase().equals("Susan".toString().trim().toLowerCase())){

                                myTv.setText(person3[bCount]);

                                if (bCount < person3.length -1){
                                    bCount++;
                                }else {
                                    bCount = 0;

0 个答案:

没有答案