Android之人品计算器教程
来源:互联网 发布:张麟征 知乎 编辑:程序博客网 时间:2024/06/06 13:18
今天来做一个人品计算器,所谓人品计算器,就是输入你的名字,随机值测试你的人品值,仅供娱乐。那么完成这样一个计算器,首先需要一个主页面和一个结果页面,即需要两个Activity…首先在drawable文件夹下放一张“人品计算器”图片(rp.jpg),再到activity_main.xml编写如下代码:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/rp" android:layout_centerHorizontal="true"/> <Button android:id="@+id/btn_calc" android:onClick="enter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="点击进入人品计算器"/> <EditText android:id="@+id/et_name" android:hint="请输入你要测试的名字" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/btn_calc" android:layout_centerHorizontal="true"/>
主页面如图,新建一个CalcActivity.java,再到MainActivity设置
public class MainActivity extends Activity { private EditText et_Name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_Name = (EditText) findViewById(R.id.et_name); } public void enter(View view) { String name = et_Name.getText().toString().trim(); //获取内容 if(TextUtils.isEmpty(name)) //如果没有输入信息点击按钮就提示 { Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show(); return; } Intent intent = new Intent(MainActivity.this,CalcActivity.class); //创建了一个空的意图 intent.putExtra("name", name); //传递数据 startActivity(intent); }}
再新建一个actvity_calc.xml
<TextView android:id="@+id/tv_result" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是计算器页面" />
其中属性(android:text=”我是计算器页面”)文字怎么写不要紧,后面会更新的
然后到CalcActivity.java编写如下代码
public class CalcActivity extends Activity { private TextView tv_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calc); tv_result = (TextView) findViewById(R.id.tv_result); Intent intent = getIntent(); //得到传递过来的Intent String name = intent.getStringExtra("name"); //定义了一个字节数组result,这个字节数组是将字符串转换成字节数组存放在result中。 byte[] result = name.getBytes(); int number=0; //将byte数组里面的每个byte转换成十进制数加起来 for(byte b : result){ //与 0xff 做 & 运算会将 byte 值变成 int 类型的值,也将 -128~0 间的负值都转成正值了。 //0xff二进制就是1111 1111 number += b&0xff;//byte原来的负值变成正的 } //名字可能为汉字,比较长,可能得到负数,故需要变为正数 int score =Math.abs(number)%100; //Math.abs(int a)返回一个int值的绝对值 if(score<60) { tv_result.setText(name+"的人品是"+score+"\n"+"你的人品太差了,你应该有干坏事的嗜好吧?"); }else if(score>=60 && score<80){ tv_result.setText(name+"的人品是"+score+"\n"+"你这人品还行吧!"); }else if(score>80 && score<100){ tv_result.setText(name+"的人品是"+score+"\n"+"哎呦!不错哦!"); }else if(score == 100){ tv_result.setText(name+"的人品是"+score+"\n"+"你这人品是要爆棚啊!"); } }}
你当然可以用Random这个来产生随机值,不过你要想个方案哦,因为同一个名字测试几次会出现不同的结果,你要排除这种可能。然后就是对结果的处理了,如上代码,if..else..语句,在这里我就不细分了,0-100区间你可以细分再细分,再加不同的评语,甚至加图片,才使人品计算器更好玩。
当然,还没完成呢,新建一个Actvity还要到AndroidManifest.xml注册
<application ......> <activity android:name=".CalcActivity" android:label="@string/title_activity_calc" > ... </application>
这样人品计算器就完成了。接下来试试看(仅供娱乐)
0 0
- Android之人品计算器教程
- Android初级教程人品计算器
- Android简单人品计算器
- android人品计算器小案列
- Android 人品计算器
- ~~~~~~~~~~~~~人品计算器~~~~~~~~~~~~~~~~~~~~~~~
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- 人品计算器
- CodeForces-702D Road to Post Office(初中数学脑洞题)
- 论dts中的of_platform_populate如何选择性加载device node为platform device
- 0.5px边框学习
- 用IRC协议与PHP木马“聊天” 20160809
- 异或操作实现两数值交换和其中的bug
- Android之人品计算器教程
- 计算数字数组中元素的最值 & KVC用法延伸
- poj 3026 Borg Maze(最小生成树)
- Spring review--动态代理和AOP代码的演化
- Linux日志文件总结
- Spring全局加解密
- JAVA设计模式-模板模式
- 计时器
- Intel x86 Atom System Image