我的应用程序中有一个文本视图、一个编辑文本和一个按钮。我想要,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