-3

我的应用程序中有一个文本视图、一个编辑文本和一个按钮。我想要,textview 数字(1-10)随机变化,用户输入相同的数字但字符,好像 textview 显示 7 所以用户必须输入值为 7。如果相同,则 textview 再次随机更改,新值应显示 textview,如果值错误,则再次显示相同的过程,因此应显示“重试”消息...
这是我的 xml 代码..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tvShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="5"
        android:textSize="100sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:text="Check" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnShow"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="26dp"
        android:ems="10"
        android:hint="Spelling"  >
        <requestFocus/>

        </EditText>

</RelativeLayout>

这是我的java代码...

public class MainActivity extends Activity implements OnClickListener{

    TextView tv;
    EditText etTextField;
    Button btnCheck;
    String val;

    Random rand= new Random();
    int a1 = rand.nextInt(10)+1;

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

        tv = (TextView) findViewById(R.id.tvShow);
        etTextField = (EditText) findViewById(R.id.editText1);
        btnCheck = (Button) findViewById(R.id.btnShow);
        btnCheck.setOnClickListener(this);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch(v.getId())
    {
    case R.id.btnShow:

        a1 = rand.nextInt(10)+1;
        tv.setText(Integer.toString(a1));
        val = etTextField.getText().toString();

        if(a1==4 && val.equals("four"))
        {

            Toast t = Toast.makeText(MainActivity.this, "4", Toast.LENGTH_SHORT);
            t.show();

        }
        val = etTextField.getText().toString();
         if(a1==7 && val.equals("seven"))
        {

            Toast t = Toast.makeText(MainActivity.this, "7", Toast.LENGTH_SHORT);
            t.show();   

        }
        val = etTextField.getText().toString();
         if(a1==8 && val.equals("eight"))
        {

            Toast t = Toast.makeText(MainActivity.this, "8", Toast.LENGTH_SHORT);
            t.show();

        }

        break;



        }//swtich
    }//onclick

}//class body
4

2 回答 2

0

Switch: case 是一种方法。但最好的方法是创建一个 hashmap,如下所示:

使用您的值创建一个 xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="numbers_string">
            <item>one</item>
            <item>two</item>//...

        </string-array>

        <integer-array name="numbers_int">
            <item>1</item>
            <item>2</item>//....
        </integer-array>
    </resources>

和代码:

这是您需要的代码...

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

switch(v.getId())
{
case R.id.btnShow:

    a1 = rand.nextInt(10)+1;
    tv.setText(Integer.toString(a1));
    val = etTextField.getText().toString();


    String[] numbersString = getResources().getStringArray(R.array.numbers_string);
    int[] numbersInt = getResources().getIntArray(R.array.numbers_int);

  //the error that you had happened because this should be <int,string> , now it works  
HashMap<Integer, String> myMap = new HashMap<Integer, String>();
for (int i = 0; i < numbersString.length; i++) 
    {
    myMap.put(numbersInt[i], numbersString[i]);
    }

   if (val.equalsIgnoreCase(myMap.get(a1))) {
     Toast t = Toast.makeText(MainActivity.this, ""+a1, Toast.LENGTH_SHORT);
      t.show();
   }
  else{
      Toast t = Toast.makeText(MainActivity.this, "Try again", Toast.LENGTH_SHORT);
      t.show();
  }

  break;


}}}    
于 2013-10-17T17:46:49.080 回答
-1

更新了你的整个代码,这对你有用

活动

public class ActivityMain extends Activity implements OnClickListener {

    TextView tv;
    EditText etTextField;
    Button btnCheck, btnReset;
    String val;
    int a1; 
    Random rand = new Random();


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

        tv = (TextView) findViewById(R.id.tvShow);
        etTextField = (EditText) findViewById(R.id.editText1);
        btnCheck = (Button) findViewById(R.id.btnShow);
        btnReset = (Button) findViewById(R.id.btnreset);
        btnCheck.setOnClickListener(this);
        btnReset.setOnClickListener(this);
        a1 = rand.nextInt(10) + 1;
        tv.setText(Integer.toString(a1));
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.btnShow:

            tv.setText(Integer.toString(a1));

            val = etTextField.getText().toString().trim();

            System.out.println("integer is " + a1);
            System.out.println("value  is " + val);

            if (a1 == 1 && val.equalsIgnoreCase("one")) {

                Toast t = Toast.makeText(ActivityMain.this, "1",
                        Toast.LENGTH_SHORT);
                t.show();

            }

            else if (a1 == 2 && val.equalsIgnoreCase("two")) {

                Toast t = Toast.makeText(ActivityMain.this, "2",
                        Toast.LENGTH_SHORT);
                t.show();

            }

            else if (a1 == 3 && val.equalsIgnoreCase("Three")) {

                Toast t = Toast.makeText(ActivityMain.this, "3",
                        Toast.LENGTH_SHORT);
                t.show();

            }

            else if (a1 == 5 && val.equalsIgnoreCase("five")) {

                Toast t = Toast.makeText(ActivityMain.this, "5",
                        Toast.LENGTH_SHORT);
                t.show();

            }

            else if (a1 == 4 && val.equalsIgnoreCase("four")) {

                Toast t = Toast.makeText(ActivityMain.this, "4",
                        Toast.LENGTH_SHORT);
                t.show();

            }

            else if (a1 == 7 && val.equalsIgnoreCase("seven")) {

                Toast t = Toast.makeText(ActivityMain.this, "7",
                        Toast.LENGTH_SHORT);
                t.show();

            }

            else if (a1 == 8 && val.equalsIgnoreCase("eight")) {

                Toast t = Toast.makeText(ActivityMain.this, "8",
                        Toast.LENGTH_SHORT);
                t.show();

            } else if (a1 == 6 && val.equalsIgnoreCase("six")) {

                Toast t = Toast.makeText(ActivityMain.this, "6",
                        Toast.LENGTH_SHORT);
                t.show();

            } else if (a1 == 9 && val.equalsIgnoreCase("nine")) {

                Toast t = Toast.makeText(ActivityMain.this, "9",
                        Toast.LENGTH_SHORT);
                t.show();

            } else if (a1 == 10 && val.equalsIgnoreCase("ten")) {

                Toast t = Toast.makeText(ActivityMain.this, "10",
                        Toast.LENGTH_SHORT);
                t.show();

            } else {

            Toast t = Toast.makeText(ActivityMain.this, "Try again",
                    Toast.LENGTH_SHORT);
            t.show();


             a1 = rand.nextInt(10) + 1;
             tv.setText(Integer.toString(a1));
             etTextField.setText("");
        }

            break;
        case R.id.btnreset:
            // update text here
             a1 = rand.nextInt(10) + 1;
             tv.setText(Integer.toString(a1));
             etTextField.setText("");

            break;
        }// swtich
    }// onclick
}// class body

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tvShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="5"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="100sp" />

    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:text="Check" />

    <Button
        android:id="@+id/btnreset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:layout_toRightOf="@+id/btnShow"
        android:text="Reset" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnShow"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="26dp"
        android:ems="10"
        android:hint="Spelling" >

        <requestFocus />
    </EditText>

</RelativeLayout>
于 2013-10-17T17:29:01.353 回答