android开发笔记————简易随机器的制作
来源:互联网 发布:健康大数据定义 编辑:程序博客网 时间:2024/05/16 02:51
这几天无聊,于是做了个随机器,想着以后可能会用的到。
以下是程序的运行截图
图1可以通过右上角的菜单转到图2 的Activity 。这个程序提供了两个功能,一个是输入姓名后随机出姓名。另一个是产生随机数。
—————————————————————————————————————————————————————————————————————————————
好的,下面来看下怎么做吧:
制作工具选的是Android studio.
创建工程的时候选择Basic activity作为我们的第一个界面,因为Basic activity 提供了 右上角的菜单选项,可以来减少工程量。
首先把各个Activity之间连接好。先右键新建两个Empty activity 。一个用来做随机数生成器的activity 一个用来做帮助的activity。
右上角的菜单可以通过以下代码来修改。通过onOptionsItemSelected 方法
@Override public boolean onOptionsItemSelected(MenuItem item) { //右上角的菜单 // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { //这里是简单的随机数生成器 Intent toEasy = new Intent(this,Main2Activity_easy.class); startActivity(toEasy); Toast.makeText(MainActivity.this, "现在是随机数生成模式", Toast.LENGTH_SHORT).show(); return true; }else if(id == R.id.help){ //这里是帮助的activity Intent toHelp= new Intent(this,Main3Activity_help.class); startActivity(toHelp); return true; } return super.onOptionsItemSelected(item); }
这样就可以通过第一个界面加载到随机数生成界面或者帮助界面了。但是还有一个问题是如何返回。
这里,我选择用向右滑动来返回。
右划返回,主要的方法是 通过OnTouchListener接口来实现。
下面以 帮助 的代码来示范(部分无关代码删除)
package com.example.cw.rands;import android.content.Intent;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class Main3Activity_help extends AppCompatActivity implements View.OnTouchListener{ //手指滑动的最小速度 private static final int X_MinSpeed = 200; //手指滑动最小距离 private static final int X_MinDistance = 150; //手指落下时的横坐标 private float X_Down; //手指移动时的横坐标 private float X_Move; //手指拿起时的横坐标 private float X_away; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3_activity_help); LinearLayout ll = (LinearLayout)findViewById(R.id.help_layout); ll.setOnTouchListener(this); } @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()){ case MotionEvent.ACTION_DOWN:{ X_Down=motionEvent.getX();//获取落下点的x break; } case MotionEvent.ACTION_UP: X_away=motionEvent.getX(); //获取移动的x int distanceX = (int)(X_away-X_Down); if(distanceX>X_MinDistance){<span style="white-space:pre"></span>//如果落下点和离开点的水平距离大于阈值,就退出这个activity finish(); return true; } break; default: break; } return true; }}
上面代码的注释应该已经说明的很清楚了。这里就不再累述了。
把 随机数生成器 和 帮助 里面都加上右滑返回后,我们试着运行下程序,一个基本的框架就弄好了。
下面我们开始实现程序的功能。
首先从简单的随机数生成器开始。
首先来到对应的layout里修改它的布局文件。这里我们用的线性布局。
通过多个 LinearLayout 的嵌套来完成布局。
下面是随机数生成器布局代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" tools:context="com.example.cw.rands.Main2Activity_easy" android:id="@+id/Easy_layout" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/EasyEditText" android:hint="请输入随机数最大值" android:layout_weight="1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/EasyInputButtom" android:layout_weight="3" android:text="确定"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="此时的范围 [ 0 , ? )" android:gravity="center" android:id="@+id/EasyMaxNumber"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/EasyRandButton" android:text="随机" android:textSize="30sp" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/EasyRandNumber" android:text="此处显示随机数" android:textSize="30sp" /></LinearLayout>
写完布局后下面就开始把里面的功能实现吧。
下面是随机数生成器的JAVA代码
package com.example.cw.rands;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import org.w3c.dom.Text;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main2Activity_easy extends AppCompatActivity implements View.OnTouchListener{ //手指滑动的最小速度 private static final int X_MinSpeed = 200; //手指滑动最小距离 private static final int X_MinDistance = 150; //手指落下时的横坐标 private float X_Down; //手指移动时的横坐标 private float X_Move; //手指拿起时的横坐标 private float X_away; //确定按钮 private Button inputButton; //随机按钮 private Button randButton; //输入框 private EditText editText; //范围显示文本 private TextView maxNumberText; //输出文本 private TextView outputText; String text=null; int maxnum=1; Long lll=2147483648L; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2_activity_easy); //实例化各个部件 inputButton=(Button)findViewById(R.id.EasyInputButtom); randButton=(Button)findViewById(R.id.EasyRandButton); editText=(EditText)findViewById(R.id.EasyEditText); maxNumberText= (TextView) findViewById(R.id.EasyMaxNumber); outputText= (TextView) findViewById(R.id.EasyRandNumber); //实例化结束 //设置确定范围的按钮监听事件 inputButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { text=editText.getText().toString(); Pattern p = Pattern.compile("[0-9]*"); Matcher m = p.matcher(text); if(text==null|text.equals("")){ //输入为空时 Toast.makeText(Main2Activity_easy.this,"输入为空!",Toast.LENGTH_SHORT).show(); return; }else if(!m.matches()){ //非法输入 Toast.makeText(Main2Activity_easy.this,"请输入小于 2147483648 且大于1的合法整数!",Toast.LENGTH_SHORT).show(); return; } else if(text.length()>10){ //非法输入 大于int的范围 Toast.makeText(Main2Activity_easy.this,"请输入小于 2147483648 且大于1的合法整数!",Toast.LENGTH_SHORT).show(); return; } else { //测试当数据为10位时的大小 用Long Long test= Long.parseLong(text); if(test>=lll){ //如果超过int范围 Toast.makeText(Main2Activity_easy.this,"请输入小于 2147483648 且大于1的合法整数!",Toast.LENGTH_SHORT).show(); return; } //测试结束 //下面开始 显示范围 editText.setText(""); maxnum = Integer.parseInt(text); maxNumberText.setText("此时的范围 [ 0 , "+maxnum+")"); //显示成功 } } }); //下面开始设置随机按钮的监听事件 randButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int randNumbers = (int)(Math.random()*maxnum); outputText.setText("本次随机数为: "+randNumbers); } }); //下面是滑动屏幕返回的监听事件 LinearLayout ll = (LinearLayout)findViewById(R.id.Easy_layout); ll.setOnTouchListener(this); } @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()){ case MotionEvent.ACTION_DOWN:{ X_Down=motionEvent.getX();//获取落下点的x break; } case MotionEvent.ACTION_UP: X_away=motionEvent.getX(); //获取移动的x int distanceX = (int)(X_away-X_Down); if(distanceX>X_MinDistance){ Toast.makeText(this,"离开随机数生成模式",Toast.LENGTH_LONG).show(); finish(); return true; } break; default: break; } return true; }}
注释也比较清楚了。这里为了防止输入不符合规范,所以规定了只能输入整数,别的是无法输入进输入框的。同时为了防止超出int的范围,又对输入的长度和大小做了判断,加以限制。
随机数生成器完成后,我们就开始弄我们最精华的部分了——按照名称来随机。
首先开始一样,先布局,下面是布局的代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.cw.rands.MainActivity" tools:showIn="@layout/activity_main" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mainEditText" android:hint="请输入随机对象名称" android:layout_weight="1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mainInputButton" android:text="添加" android:layout_weight="3"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mainShowNameText" android:text="此时随机对象有: "/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mainDelOneNameButton" android:text="删除上一个对象!" android:layout_weight="1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mainClearAllNameButton" android:layout_weight="1" android:text="清空所有对象!"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mainRandButton" android:text="随机!" android:textSize="30sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mainRandText" android:textSize="20sp"/> </LinearLayout></LinearLayout>
完成布局后开始着手为这些部件添加功能代码了。
下面是代码
package com.example.cw.rands;import android.content.Intent;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import java.util.HashMap;import java.util.Map;public class MainActivity extends AppCompatActivity{ //输入框 private EditText editText; //添加按钮 private Button addButton; //显示所有姓名的文本 private TextView nameText; //删除按钮 private Button delButton; //清空按钮 private Button clearButton; //随机按钮 private Button randButton; //随机姓名显示文本 private TextView randText; //map里的key值 private int key=0; //输入框的文本内容 private String edit; //输入文本框的总内容 private String sumText; //名字计数器 private int i=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //下面开始实例化 //输入框 editText=(EditText)findViewById(R.id.mainEditText); //添加按钮 addButton=(Button) findViewById(R.id.mainInputButton); //显示所有姓名的文本 nameText=(TextView)findViewById(R.id.mainShowNameText); //删除按钮 delButton=(Button)findViewById(R.id.mainDelOneNameButton); //清空按钮 clearButton=(Button)findViewById(R.id.mainClearAllNameButton); //随机按钮 randButton=(Button)findViewById(R.id.mainRandButton); //随机姓名显示文本 randText=(TextView)findViewById(R.id.mainRandText); //实例化结束 //下面输入的名字用map来存储 final Map<Integer,String> map = new HashMap<Integer,String>(); i=0;//计数器清零 //设置添加按钮的监听事件 addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { edit=editText.getText().toString(); if(edit.equals("")||edit==null){ //判断输入是否为空 Toast.makeText(MainActivity.this,"输入为空!",Toast.LENGTH_SHORT).show(); return; } if(sumText==null){ sumText=" "+edit; editText.setText(""); }else { sumText=sumText+" 、"+edit; editText.setText(""); } map.put(i,edit); //向map中添加有效数据 i++; //key值增加 nameText.setText("此时随机对象有: "+sumText); } }); //删除按钮的监听事件 delButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(i>0){ i--; //key值减一,根据 hashmap特性,将来添加数据会覆盖在之前的key和value值上 int i=sumText.lastIndexOf(" "); sumText=sumText.substring(0,i); nameText.setText("此时随机对象有: "+sumText); randText.setText(""); return; }else { Toast.makeText(MainActivity.this,"没有随机对象",Toast.LENGTH_SHORT).show(); randText.setText(""); return; } } }); //清空按钮的监听事件 clearButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(sumText==null){ Toast.makeText(MainActivity.this,"没有随机对象",Toast.LENGTH_SHORT).show(); return; } sumText=null; nameText.setText("此时随机对象有: "); randText.setText(""); i=0; map.clear(); return; } }); //随机按钮的监听事件 randButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(i==0){ Toast.makeText(MainActivity.this,"请输入要随机的对象名称!",Toast.LENGTH_SHORT).show(); return; } int randNumbers = (int)(Math.random()*i); String str =map.get(randNumbers).toString(); randText.setText("本次随机结果为: "+str); } }); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "更多问题查看右上角帮助", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { //右上角的菜单 // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { //这里是简单的随机数生成器 Intent toEasy = new Intent(this,Main2Activity_easy.class); startActivity(toEasy); Toast.makeText(MainActivity.this, "现在是随机数生成模式", Toast.LENGTH_SHORT).show(); return true; }else if(id == R.id.help){ //这里是帮助的activity Intent toHelp= new Intent(this,Main3Activity_help.class); startActivity(toHelp); return true; } return super.onOptionsItemSelected(item); }}
代码很长。。但注释也算是比较清楚的了。看懂应该不难。用了Hashmap来存放姓名数据。
接着还有一个 帮助 的activity。相信如果前面两个会做的话,这个应该很快的。
下面是布局代码和功能代码,其中加入了 反馈 和 报警 。增加趣味性。。
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" tools:context="com.example.cw.rands.Main3Activity_help" android:id="@+id/help_layout" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用说明" android:gravity="center" android:textSize="25sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1、简易随机器的使用" android:textSize="18sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text=" 在编辑框中输入要随机的对象名称后,按添加按钮将其添加到随机池中。删除按钮可以删除上一个随机对象,清空按钮清空所有随机对象名称。随机按钮显示随机结果。 "/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2、随机数生成器的使用" android:textSize="18sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text=" 向右滑动可以返回!帮助也支持! 在编辑框中输入要随机的数值最大值后按确定,程序会把随机值域定为 [0,输入的随机数)。右侧为开区间!按随机按钮开始随机! "/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3、一些说明" android:textSize="18sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text=" 虽然做了一些防止输错的处理,但还是希望大家能在输入的时候能规范的输入。 该程序为兴趣作品。初次试手,总有些不足之处(光是这界面就够辣眼睛的 ˊ _>ˋ )还望多多见谅。 意见和建议以及一些BUG反馈,请戳下面的 反馈 或者 报警! "/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Call" android:layout_weight="1" android:text="反馈" android:layout_gravity="center" android:textSize="20sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/CallPlic" android:layout_weight="1" android:text="报警" android:layout_gravity="center" android:textSize="20sp"/> </LinearLayout></LinearLayout>
package com.example.cw.rands;import android.content.Intent;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class Main3Activity_help extends AppCompatActivity implements View.OnTouchListener{ //手指滑动的最小速度 private static final int X_MinSpeed = 200; //手指滑动最小距离 private static final int X_MinDistance = 150; //手指落下时的横坐标 private float X_Down; //手指移动时的横坐标 private float X_Move; //手指拿起时的横坐标 private float X_away; //和我联系 private TextView callMe; //报警 private TextView call110; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3_activity_help); callMe=(TextView)findViewById(R.id.Call); call110=(TextView)findViewById(R.id.CallPlic); callMe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String url="mqqwpa://im/chat?chat_type=wpa&uin=联系人的qq号代替,此处我删掉了"; startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } }); call110.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + "110")); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }); LinearLayout ll = (LinearLayout)findViewById(R.id.help_layout); ll.setOnTouchListener(this); } @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()){ case MotionEvent.ACTION_DOWN:{ X_Down=motionEvent.getX();//获取落下点的x break; } case MotionEvent.ACTION_UP: X_away=motionEvent.getX(); //获取移动的x int distanceX = (int)(X_away-X_Down); if(distanceX>X_MinDistance){ finish(); return true; } break; default: break; } return true; }}
那么,至此,我们全部的工作基本都完成了。为什么说基本呢?
因为还要自己安装起来看看有没有bug和不太友好的地方要修该。
—————————————————————————————————————————————————————————————————————————————
后记
算是第一次自己独立完成这么一个Android应用吧。之前在写计算器应用的时候我还是各种百度,跌跌撞撞才完成,那时对整个一个制作还不是很熟悉。
这次这个程序难度大于计算器,但是大部分代码都是自己直接敲进去的,很少百度,看来熟练了很多。
这条路还很长,慢慢来吧。
- android开发笔记————简易随机器的制作
- Android开发笔记之简易画画板的制作
- android开发笔记之网络编程—简易新闻客户端
- Android开发——本地验证码的简易实现
- SwiSH max——简易动画制作的最佳选择
- 新型随机函数——乱排函数的制作
- Android开发的欢迎界面制作的简易教程
- Android笔记(十九)制作一个简易的指南针
- Git开发笔记——搭建Git简易高效服务器
- Android开发——实现Android简易计算器
- Android基础——简易的Notification
- Android学习笔记五—简易新闻应用设计
- Android开发之—.9.png图片的制作
- Android笔记—如何制作欢迎界面
- 《ArcGIS Runtime SDK for Android开发笔记》——数据制作篇:紧凑型切片制作(Server缓存切片)
- 随机森林—算法笔记
- Android学习笔记(七) —— 简易的按钮事件 — Button事件处理
- android开发中WebView控件的实例与注意要点——个人主页浏览器简易实现
- 搭建个人博客完整教程
- 程序设计与算法 第二周测验
- VS中西红柿插件的打开
- hdu 3008 Warcraft
- android 开发,如何从自己的APP打开另一个APP
- android开发笔记————简易随机器的制作
- java js传值乱码
- 面试技能点整理
- javascript ECMAScript 原型
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 技能点整理—2
- 夜
- poj 1611 The Suspects
- redis 命令收集