安卓开发:Guess游戏
来源:互联网 发布:微山湖武工队知乎 编辑:程序博客网 时间:2024/05/16 02:03
这是我们学安卓开发第一个小软件啦。猜数字游戏,^.^
刚开始确定很懵,这是啥的,课后自己又多敲了敲,百度才理解啥意思。
刚开始学真的太懵了,老师直接一股脑全部灌进来。不复习百度还真不行
好啦,进入正题
建立好Activity,拖好按钮,自己打代码设置按钮也可以,我比较懒啦
在MainActivity里面创建button,editview,textview按钮,用来查找窗体中对应的按钮
but=(Button)findViewById(R.id.button1);
edi=(EditText)findViewById(R.id.editText1);
txt=(TextView)findViewById(R.id.textView1);
对but进行响应事件,获得edi里面的值比较一下就好了
package com.example.guess;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity { private Button but; private EditText edi; private TextView txt; private int num;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);but=(Button)findViewById(R.id.button1);edi=(EditText)findViewById(R.id.editText1);txt=(TextView)findViewById(R.id.textView1);but.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO 自动生成的方法存根if(but.getText().toString().equals("确定")){if(edi.getText().toString().equals("")){return;}int n=Integer.parseInt(edi.getText().toString());if(n>num){txt.setText("猜大了,再试试");edi.setText("");}else if(n==num){txt.setText("恭喜你猜对了!");but.setText("再来一次");edi.setText("");}else{txt.setText("猜小了,再试试");edi.setText("");}}else{newgame();}}});newgame();}@Overridepublic 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;} private void newgame(){ num=(int) (Math.random()*9+1); txt.setText("请输入1--10的数字"); but.setText("确定"); edi.setText(""); }}
0 0
- 安卓开发:Guess游戏
- 安卓游戏开发大纲
- 安卓游戏开发--策略地图游戏
- 面试安卓游戏开发岗位
- Android安卓手机游戏开发
- 安卓cocos2d游戏开发1.0
- Unity安卓游戏开发优化
- Unity安卓VR游戏开发
- 如何开发安卓游戏(转)
- 安卓游戏开发之【扎金花游戏】(一)
- 安卓游戏开发之【扎金花游戏】(二)
- 安卓游戏开发之【扎金花游戏】(三)
- 安卓游戏开发之【扎金花游戏】(四)
- Python guess 游戏
- Unity开发安卓游戏(2):基于安卓游戏的优化方案总结!
- Unity开发安卓游戏(2):基于安卓游戏的优化方案总结!
- 安卓游戏开发------让游戏人物动起来(游戏帧动画的处理 )(一)
- 2012.9.12 安卓游戏开发笔记(一)_Button
- jQuery实现点击元素以外的地方隐藏该元素
- 小白第一天
- 面试题:“你能不能谈谈,java GC是在什么时候,对什么东西,做了什么事情?”
- 双流网络行为识别-Spatiotemporal Residual Networks for Video Action Recognition-论文阅读
- 一起做剑指offer吧!
- 安卓开发:Guess游戏
- JavaSE 学习参考:方法的重载
- base64后台传输细节处理
- 【BZOJ】3130: [Sdoi2013]费用流
- Scala语言与Play框架入门教程
- JVM和Java历史
- leecode 解题总结:345. Reverse Vowels of a String
- 小王的尴尬日常(二)---Openssl 实现国密算法(基础介绍和产生秘钥对)
- 平面上的地图搜索--Java学习笔记(四)