自定义数字范围猜数
package zhang.app.myGuess;
import zhang.guess.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class guess extends Activity {
private Button ok;
private EditText min;
private EditText max;
private Button guessButton;
private TextView showText;
private EditText gEditText;
public int down=1;
public int up=100;
private int random=0;
private int i=1;
private Vibrator vibrator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.guess_main);
min=(EditText)findViewById(R.id.getMin);
max=(EditText)findViewById(R.id.getMax);
gEditText=(EditText)findViewById(R.id.guessText);
ok=(Button)findViewById(R.id.ok);
guessButton=(Button)findViewById(R.id.guessButton);
showText=(TextView)findViewById(R.id.showText);
ok.setOnClickListener(new okClickListener());
guessButton.setEnabled(false);
gEditText.setEnabled(false);
guessButton.setOnClickListener(new guessButtonListener());
}
class okClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String numa=String.valueOf(min.getText().toString());
String numb=String.valueOf(max.getText().toString());
if(numa.equalsIgnoreCase("")||numb.equalsIgnoreCase("")){
Toast.makeText(guess.this, "请正确输入", Toast.LENGTH_SHORT).show();
}
else if(Integer.parseInt(min.getText().toString())>Integer.parseInt(max.getText().toString())){
Toast.makeText(guess.this, "输入错误,上限应大于下限!", Toast.LENGTH_SHORT).show();
}
else{
guessButton.setEnabled(true);
gEditText.setEnabled(true);
ok.setEnabled(false);
down=Integer.parseInt(min.getText().toString());
up=Integer.parseInt(max.getText().toString());
min.setEnabled(false);
max.setEnabled(false);
showText.setTextColor(Color.WHITE);
showText.setText("游戏开始"+"\n");
while(true){
random = Integer.valueOf((int) (Math.random()*up));
if(random<=down){
continue;
}else{
break;
}
}
}
}
}
class guessButtonListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(gEditText.getText().toString().equalsIgnoreCase("")){
gEditText.setText("");
}
else{
int guessnum=Integer.valueOf(String.valueOf(gEditText.getText()));
if(String.valueOf(gEditText).equalsIgnoreCase("")){
Toast.makeText(guess.this, "check input3", Toast.LENGTH_SHORT).show();
}else if(guessnum>random){
showText.setTextColor(Color.LTGRAY);
int z=Math.abs(((Integer.valueOf((int) (Math.random()*up)))+20)%3);
if(guessnum>up){
showText.append("第["+i+"]次猜数--:"+guessnum+",天啊,超越上限了啊啊啊~~\n");
} else if(z==1){
showText.append("第["+i+"]次猜数--"+guessnum+",My God!,你太冲动了\n");
}else if(z==2){
showText.append("第["+i+"]次猜数--"+guessnum+",没这么大吧?\n");
}else {
if(guessnum-20>0){
showText.append("第["+i+"]次猜数--"+guessnum+",大了!,也许是"+(guessnum-9)+"\n");
}else if(guessnum-50>0){
showText.append("第["+i+"]次猜数--"+guessnum+",大了!,也许是"+(guessnum-40)+"\n");
}else if(guessnum-80>0){
showText.append("第["+i+"]次猜数--"+guessnum+",大了!,也许是"+(guessnum-70)+"\n");
}else {
showText.append("第["+i+"]次猜数--"+guessnum+",大了!啊~~\n");
}
}
}else if(guessnum<random){
showText.setTextColor(Color.LTGRAY);
int z=Math.abs(((Integer.valueOf((int) (Math.random()*up)))+10)%3);
if(guessnum<down){
showText.append("第["+i+"]次猜数--"+guessnum+",天啊 ,都比下限还小了。\n");
}else if(z==1){
showText.append("第["+i+"]次猜数--"+guessnum+",小了!,别怕哦\n");
}else if(z==2){
showText.append("第["+i+"]次猜数--"+guessnum+",小了!,真不给力\n");
}else {
showText.append("第["+i+"]次猜数--"+guessnum+",也太小了吧。\n");
}
}else if(guessnum==random){
zd();
ok.setEnabled(true);
gEditText.setEnabled(false);
guessButton.setEnabled(false);
min.setEnabled(true);
max.setEnabled(true);
showText.setTextColor(Color.YELLOW);
if(i==1){
showText.append("第"+i+"次猜数--"+guessnum+":厉害,秒杀了。\n");
}else if(i==2){
showText.append("第"+i+"次猜数--"+guessnum+":运气不错哦。\n");
}else if(i>3&&i<10){
showText.append("第"+i+"次猜数--"+guessnum+":或许可以买组体彩了。\n");
}else{
showText.append("第"+i+"次猜数--"+guessnum+":猜中啦,再来一次!\n");
}
showText.append("一共猜["+i+"]次!");
i=0;
}
else if(gEditText.getText().toString().equalsIgnoreCase("")){
}
else{
gEditText.setText("");
}
gEditText.setText("");
}
i++;
}
}
public boolean onCreateOptionsMenu(Menu menu) { //添加菜单
// TODO Auto-generated method stub
menu.add(0, 1, 1,"重置").setIcon(android.R.drawable.ic_menu_rotate);;
menu.add(0, 2, 2,"关于").setIcon(android.R.drawable.ic_menu_info_details);;
menu.add(0,3,3,"退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) { //设置菜单事件
if(item.getItemId()==1){
ok.setEnabled(true);
gEditText.setEnabled(false);
guessButton.setEnabled(false);
min.setText("1");
max.setText("100");
down=1;
up=100;
gEditText.setText("");
showText.setText("");
min.setEnabled(true);
max.setEnabled(true);
i=1;
}
else if(item.getItemId()==2){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("关于").setMessage("By:没落凄凉\n").show();
}else{
finish();
}
return super.onOptionsItemSelected(item);
}
public void zd(){ //振动的方法
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] pattern = {0, 100, 0, 0}; // OFF/ON/OFF/ON...
vibrator.vibrate(pattern, -1);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/back"
>
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tableLayout1">
<TableRow
android:layout_marginTop="10dip"
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_weight="1"
android:layout_width="fill_parent"
android:id="@+id/textView1"
android:text="下限"
android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/getMin"
android:inputType="number"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="1" android:layout_width="80dip"
android:maxLength="9"></EditText>
<TextView android:layout_weight="1"
android:layout_width="wrap_content"
android:id="@+id/textView2"
android:text="上限"
android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/getMax"
android:inputType="number"
android:layout_weight="4"
android:layout_height="wrap_content"
android:text="100"
android:maxLength="9"
android:layout_width="80dip"></EditText>
<Button
android:text="确定"
android:id="@+id/ok"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
<TableRow android:id="@+id/tableRow2" android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/guessText"
android:inputType="number"
android:layout_weight="11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></EditText>
<Button
android:text="猜"
android:id="@+id/guessButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:scrollbars="vertical" android:fadingEdge="vertical">
<TextView
android:id="@+id/showText"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:textColor="@color/red"
android:singleLine="false"
android:scrollbars="vertical"
android:typeface="serif"
></TextView>
</ScrollView>
</LinearLayout>