终极密码游戏,终极密码
投稿于 被查看 31129 次 评论:116
终极密码游戏,终极密码
<无详细内容>1.Activity
package android.m9; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class main extends Activity { /** Called when the activity is first created. */ private TextView logo; private EditText etStart, etEnd; private TextView tv; private EditText guess; private Button reset, submit; private EditText result; private int key; private int myguess; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // get id for widgets logo = (TextView) findViewById(R.id.logo); logo.setTextColor(Color.GRAY); etStart = (EditText) findViewById(R.id.etStart); etEnd = (EditText) findViewById(R.id.etEnd); tv = (TextView) findViewById(R.id.tv); tv.setText(R.string.tv); guess = (EditText) findViewById(R.id.guess); reset = (Button) findViewById(R.id.reset); submit = (Button) findViewById(R.id.submit); result = (EditText) findViewById(R.id.result); // set not enabled to input result.setEnabled(false); etStart.setEnabled(false); etStart.setText("" + 0); etEnd.setEnabled(false); etEnd.setText("" + 100); result.setEnabled(false); // random number key = (int) (Math.random() * 100); // set listener reset.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub key = (int) (Math.random() * 100); guess.setText(""); etStart.setText("0"); etEnd.setText("100"); } }); submit.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub myguess = (Integer.decode(guess.getText().toString())) .intValue(); if (key == myguess) result.setText(R.string.bingo); else { if (myguess > key) { etEnd.setText("" + myguess); } else { etStart.setText("" + myguess); } guess.setText(""); } } }); } }
2.布局xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="wrap_content" android:padding="15dip"> <TextView android:id="@+id/logo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/logo"> </TextView> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal"> <EditText android:id="@+id/etStart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:width="80dip"> </EditText> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> <EditText android:id="@+id/etEnd" android:layout_width="fill_parent" android:layout_height="wrap_content" android:width="80dip"> </EditText> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:text="@string/reset" android:layout_width="fill_parent" android:id="@+id/reset" android:layout_height="wrap_content"> </Button> <EditText android:id="@+id/guess" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numeric="integer" android:width="50dip"> </EditText> <Button android:text="@string/submit" android:layout_width="fill_parent" android:id="@+id/submit" android:layout_height="wrap_content"> </Button> </TableRow> </TableLayout> <EditText android:padding="30dip" android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:width="150dip" android:gravity="center_horizontal"> </EditText> </LinearLayout>
3.string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">FinalCode</string> <string name="logo">FinalCode</string> <string name="tv">~</string> <string name="reset">开始</string> <string name="submit">确定</string> <string name="bingo">恭喜您猜对了!^ ^</string> </resources>
用户评论