我的应用中有一个textview,一个edittext和一个按钮。
我想,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
答案 0 :(得分: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;
}}}
答案 1 :(得分:-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>