先看效果图如下:
这是一个简单的星座运势查看的APP。
先来看欢迎界面,欢迎界面很简单,只要是一个简单的动画播放效果,然后对动画播放设置监听,当动画播放结束后自动跳转到主界面。欢迎界面主要代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
iv=(ImageView) findViewById(R.id.iv);
//透明动画
alpha();
}
private void alpha() {
//得到透明动画对象,并初始化;fromAlpha:其始透明度,toAlpha:结束透明度
AlphaAnimation aa=new AlphaAnimation(0.0f, 1.0f);
//设置动画时间
aa.setDuration(3000);
//设置播放重复的次数
aa.setRepeatCount(0);
//设置播放的模式:RESTART:从新开始 REVERSE:反转
aa.setRepeatMode(AlphaAnimation.RESTART);
//开始执行动画;View中执行动画的方法
iv.startAnimation(aa);
//设置监听
aa.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
//动画播放结束后执行的函数
@Override
public void onAnimationEnd(Animation animation) {
Intent intent=new Intent();
intent.setClass(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
主界面只要是对输入框的输入的内容判断、返回键的设置、网络的判断。主要代码如下:
//对返回键进行监听,双击返回键时退出APP(注意的是要在欢迎页面对生命周期的onstop()方法内调用finish()方法才不会再返回欢迎界面)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
exit();
return false;
}
return super.onKeyDown(keyCode, event);
}
//退出方法
private void exit() {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
Toast.makeText(MainActivity.this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
} else {
//用户退出处理
finish();
System.exit(0);
}
}
// 跳转到结果界面以及将输入宽的内容传递到parmas中
private void intent() {
String ms = etmsg.getText().toString();
if (ms == null || (ms.length()) == 0) {
Toast.makeText(this, "请输入星座", Toast.LENGTH_SHORT).show();
} else {
if (ms.equals("水瓶座") || ms.equals("双鱼座") || ms.equals("白羊座")
|| ms.equals("金牛座") || ms.equals("双子座") || ms.equals("巨蟹座")
|| ms.equals("狮子座") || ms.equals("处女座") || ms.equals("天秤座")
|| ms.equals("天蝎座") || ms.equals("射手座") || ms.equals("摩羯座")) {
// 判断网络是否连接
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cwjManager.getActiveNetworkInfo();
boolean netSataus = true;
if (info != null) {
netSataus = info.isAvailable();
intent.putExtra("ms", ms);
startActivity(intent);
} else {
Toast.makeText(this, "网络已经断开", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "请输入正确的星座", Toast.LENGTH_SHORT).show();
}
}
}
显示结果界面就是简单的将json数据进行解析并设置显示。注意的是要在线程中去获取json数据。主要代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_result);
// 初始化控件
init();
//得到json数据并将数据发送
getResult();
}
private void init() {
tv_star = (TextView) findViewById(R.id.tv_star);
tv_date = (TextView) findViewById(R.id.tv_date);
tv_lucky = (TextView) findViewById(R.id.tv_lucky);
tv_healthy = (TextView) findViewById(R.id.tv_healthy);
tv_love = (TextView) findViewById(R.id.tv_love);
tv_work = (TextView) findViewById(R.id.tv_work);
tv_money = (TextView) findViewById(R.id.tv_money);
tv_all = (TextView) findViewById(R.id.tv_all);
tv_tips = (TextView) findViewById(R.id.tv_tips);
}
//解析json数据并将其显示
// 首先定义一个Handler对象
private Handler mhandle = new Handler() {
// 处理消息的方法
public void handleMessage(Message msg) {
// 接收消息
switch (msg.what) {
case 1:// 处理消息
String strmsg = (String) msg.obj;
try {
JSONObject person = new JSONObject(strmsg);
String name = person.getString("name");
String date = person.getString("date");
String lucky=person.getString("color");
String heathly=person.getString("health");
String love=person.getString("love");
String work=person.getString("work");
String money=person.getString("money");
String all=person.getString("all");
String tips=person.getString("summary");
Log.i("TAG", name);
tv_star.setText(name);
tv_date.setText(date);
tv_lucky.setText(lucky);
tv_healthy.setText(heathly);
tv_love.setText(love);
tv_work.setText(work);
tv_money.setText(money);
tv_all.setText(all);
tv_tips.setText("友情提示:"+tips);
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
};
};
//通过首页传过来的星座以及日期去得到json数据并发送
private void getResult() {
//新建一个线程主要是进行连网并获得json数据。new Thread(){}这里使用了匿名内部类的方法
new Thread() {
public void run() {
Intent intent = getIntent();
String result = null;
String url = "http://web.juhe.cn:8080/constellation/getAll";// 请求接口地址
Map params = new HashMap();// 请求参数
params.put("key", APPKEY);// 应用APPKEY(应用详细页查询)
//得带首页传过来的星座
String etmsg_date = intent.getStringExtra("ms");
params.put("consName", etmsg_date);// 星座名称,如:白羊座
//得到首页要查询的日期
String date = intent.getStringExtra("date");
params.put("type", date);// 运势类型:today,tomorrow,week,nextweek,month,year
//将url, params, "GET"传到HTTPUtil进行json数据的获取,得带json格式的数据
result = HttpUtils.net(url, params, "GET");
Log.i("TAG", result);
Message msg = Message.obtain();
// 消息标记(类似于邮票)
msg.what = 1;
// 发送消息
msg.obj = result;
mhandle.sendMessage(msg);
}
}.start();
}
这里只是简单的展示一下代码,还有很多细节要注意。如果需要源码和制作过程可添加小编微信获取:caiwencai123456