又是隔了好一阵子,这次带来的是当时写的一个应用——星座达人,提供一系列的综合服务,也就是脑子一热,就想写个星座题材的应用。
应用截图:
具体的内容,可以下载查看:http://www.anzhi.com/soft_2473845.html
项目工程结构
包com.eric.activity:放置一系列的Activity页面
AboutUsActivity关于页面,简单文本;
AboutUsActivity.java
package com.eric.activity;
import com.eric.constellation.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
/*
*@author Eric
*@2015-11-24下午9:57:07
*/
public class AboutUsActivity extends Activity{
private LinearLayout layout_title_about;
private ImageView image_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_us);
layout_title_about = (LinearLayout) findViewById(R.id.layout_about);
image_back = (ImageView) findViewById(R.id.image_about_back);
layout_title_about.getBackground().setAlpha(70);
image_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
activity_about_us.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background= "@drawable/bg4"
>
<RelativeLayout
android:id="@+id/layout_title_about"
android:layout_width="fill_parent"
android:layout_height="@dimen/activity_main_title_height"
android:layout_alignParentTop="true"
android:background="@drawable/title_bg">
<TextView
android:id="@+id/text_title_about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="关于"
android:textSize="@dimen/activity_main_title_text_size"
android:layout_centerInParent="true"
android:gravity="center"/>
<ImageView
android:id="@+id/image_about_back"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="@drawable/title_back_up_down"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_about"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:padding="20dp"
android:layout_below="@id/layout_title_about"
android:background="@drawable/single_up_down"
android:layoutAnimation="@anim/layout_animation"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/aboutus"/>
<TextView
android:id="@+id/text_about_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="@dimen/activity_about_text"
android:textColor="#FFF"
android:text="@string/app_name"
/>
<TextView
android:id="@+id/text_about_version"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="@dimen/activity_about_text"
android:textColor="#FFF"
android:text="@string/version_name"
/>
<TextView
android:id="@+id/text_about_author"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="@dimen/activity_about_text"
android:textColor="#FFF"
android:text="@string/author"
/>
<TextView
android:id="@+id/text_about_mail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="@dimen/activity_about_text"
android:textColor="#FFF"
android:text="@string/mail"
/>
<TextView
android:id="@+id/text_about_copyright"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="@dimen/activity_about_text"
android:textColor="#FFF"
android:text="@string/copyright"
/>
</LinearLayout>
</RelativeLayout>
BottomMenuActivity下方弹出菜单页;
BottomMenuActivity.java自定义的一个下弹页面,当做对话框使用,计算积分,后续没有触发;
package com.eric.activity;
import com.eric.constellation.R;
import com.eric.test.AppConnect;
import com.eric.test.UpdatePointsListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/*
*@author Eric
*@2015-11-16下午7:25:28
*/
public class BottomMenuActivity extends Activity implements OnClickListener,UpdatePointsListener{
private Button btn_next,btn_go;
private TextView text,tips;
private String info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_menu);
AppConnect.getInstance(this);
btn_next = (Button) findViewById(R.id.btn_next);
btn_go = (Button) findViewById(R.id.btn_go);
text = (TextView) findViewById(R.id.text_point_info);
tips = (TextView) findViewById(R.id.text_point_tips);
info = "当前星币:"+MainActivity.point+"\n"+"消耗星币:"+MainActivity.cost;
text.setText(info);
btn_next.setOnClickListener(this);
btn_go.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_next:
finish();
break;
case R.id.btn_go:
AppConnect.getInstance(this).showOffers(this);
break;
}
}
@Override
public void getUpdatePoints(String arg0, int arg1) {
MainActivity.point = arg1;
}
@Override
public void getUpdatePointsFailed(String arg0) {
}
}
BudejieActivity百思不得姐页面,获取图片和文本页面列表,下拉刷新上拉加载;
BudejieActivity.java通过异步加载框架进行请求图片和文本数据并解析Json,头像随机更新列表项;
package com.eric.activity;
import java.io.BufferedInputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.eric.constellation.R;
import com.eric.mygifview.MyGifView;
import com.eric.mylistview.MyListener;
import com.eric.mylistview.PullToRefreshLayout;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.eric.budejie.*;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
/*
*@author Eric
*@2015-11-23上午9:37:45
*/
public class BudejieActivity extends Activity{
public ImageLoader imageLoader = ImageLoader.getInstance();
private ListView listView;
private Drawable draw;
private int currentPage = 1;
private int currentList;
private String result;
private String name,create_time,love,hate,
text,type,image0;
private ArrayList<HashMap<String, Object>> dataList
= new ArrayList<HashMap<String,Object>>();
private JSONObject contentObject = null;
private MyAdapter adapter;
public static MyHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_budejie);
((PullToRefreshLayout) findViewById(R.id.refresh_view))
.setOnRefreshListener(new MyListener());
listView = (ListView) findViewById(R.id.content_view);
initListView();
}
private void initListView()
{
final AsyncHttpResponseHandler resHandler=new AsyncHttpResponseHandler(){
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable e) {
//做一些异常处理
e.printStackTrace();
Toast.makeText(BudejieActivity.this, "网络异常", 0).show();
}
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
//数据解析完添加到ListView
//System.out.println("response is :"+new String(responseBody,"utf-8"));
result = new String(responseBody,"utf-8");
readJson(result);
//System.out.println("解析到的数据:"+dataList);
adapter = new MyAdapter(BudejieActivity.this, dataList);
listView.setAdapter(adapter);
currentList = listView.getCount();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}};
new ShowApiRequest( "http://route.showapi.com/255-1", "12672", "fe08f7b8ae3f4e3cb6855304fe96bbeb")
.setResponseHandler(resHandler)
.post();
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id){
}
});
handler = new MyHandler();
}
//解析Json数据
public void readJson(String str){
try {
JSONTokener jsonParser = new JSONTokener(str);
JSONObject object = (JSONObject) jsonParser.nextValue();
JSONObject bodyObject = object.getJSONObject("showapi_res_body");
JSONObject pageObject = bodyObject.getJSONObject("pagebean");
JSONArray contentArrary = pageObject.getJSONArray("contentlist");
System.out.println("ContentArrary="+contentArrary.length());
for (int i = 0; i < contentArrary.length(); i++) {
HashMap<String,Object> map = new HashMap<String,Object>();
contentObject = contentArrary.getJSONObject(i);
type = contentObject.getString("type");
name = contentObject.getString("name");
create_time = contentObject.getString("create_time");
love = contentObject.getString("love");
hate = contentObject.getString("hate");
text = contentObject.getString("text");
switch (Integer.parseInt(type)) {
case 10://图片
image0 = contentObject.getString("image0");
//is_gif = contentObject.getString("is_gif");
map.put("image0", image0);
map.put("name", name);
map.put("create_time", create_time);
map.put("love", love);
map.put("hate", hate);
map.put("text", text.trim());
map.put("type", type);
dataList.add(map);
break;
case 29://段子
map.put("name", name);
map.put("create_time", create_time);
map.put("love", love);
map.put("hate", hate);
map.put("text", text.trim());
map.put("type", type);
dataList.add(map);
break;
case 31://声音
break;
case 41://视频
break;
}
}
} catch (Exception e) {
System.out.println("解析Json异常:"+e);
}
}
public class MyAdapter extends BaseAdapter {
ArrayList<HashMap<String, Object>> dataList;
Context context;
public MyAdapter(Context context, ArrayList<HashMap<String, Object>> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = LayoutInflater.from(context).inflate(
R.layout.layout_listview_item, null);
String name = (String)dataList.get(position).get("name");
String create_time = (String)dataList.get(position).get("create_time");
String text = (String)dataList.get(position).get("text");
String love = (String)dataList.get(position).get("love");
String hate = (String)dataList.get(position).get("hate");
String type = (String)dataList.get(position).get("type");
switch (Integer.parseInt(type)) {
case 10://图片
String image0 = (String)dataList.get(position).get("image0");
String gif = image0.substring(image0.lastIndexOf(".")+1, image0.length());
DisplayImageOptions options=new DisplayImageOptions.Builder()
//.showImageOnLoading(R.drawable.v)/*加载图片的时候显示正在加载的图*/
.showImageOnFail(R.drawable.menu_text_icon)/*加载图片失败后显示这个张图*/
.cacheInMemory(true)/*缓存至内存*/
//.cacheOnDisk(true)/*缓存值SDcard*/
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
if(gif.equals("gif")){
WebView wv = ((WebView)view.findViewById(R.id.WebView));
wv.setBackgroundColor(0);
wv.loadDataWithBaseURL(null,"<HTML><body bgcolor=transparent><div align=center>" +
"<IMG src='"+image0+"'/></div></body></html>", "text/html", "UTF-8",null);
wv.setVisibility(View.VISIBLE);
}else{
ImageView v = ((ImageView)view.findViewById(R.id.image_budejie_content));
imageLoader.init(ImageLoaderConfiguration.createDefault(BudejieActivity.this));
ImageLoader.getInstance().displayImage(image0,v,options);
}
break;
case 29://段子
break;
case 31://声音
break;
case 41://视频
break;
}
((TextView)view.findViewById(R.id.text_name)).setText(name);
((TextView)view.findViewById(R.id.text_create_time)).setText(create_time);
((TextView)view.findViewById(R.id.text_budejie_text)).setText(text);
((TextView)view.findViewById(R.id.text_love)).setText(love);
((TextView)view.findViewById(R.id.text_hate)).setText(hate);
ImageView touxiang = (ImageView)view.findViewById(R.id.image_budejie_touxiang);
randomDrawable(touxiang);
touxiang.setImageDrawable(draw);
return view;
}
}
public void randomDrawable(ImageView touxiang){
Random random = new Random();
int n = random.nextInt(12);
Resources resourses = this.getResources();
switch (n) {
case 0:
draw = resourses.getDrawable(R.drawable.view1);
break;
case 1:
draw = resourses.getDrawable(R.drawable.view2);
break;
case 2:
draw = resourses.getDrawable(R.drawable.view3);
break;
case 3:
draw = resourses.getDrawable(R.drawable.view4);
break;
case 4:
draw = resourses.getDrawable(R.drawable.view5);
break;
case 5:
draw = resourses.getDrawable(R.drawable.view6);
break;
case 6:
draw = resourses.getDrawable(R.drawable.view7);
break;
case 7:
draw = resourses.getDrawable(R.drawable.view8);
break;
case 8:
draw = resourses.getDrawable(R.drawable.view9);
break;
case 9:
draw = resourses.getDrawable(R.drawable.view10);
break;
case 10:
draw = resourses.getDrawable(R.drawable.view11);
break;
case 11:
draw = resourses.getDrawable(R.drawable.view12);
break;
}
}
public class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.arg1==1){
System.out.println("刷新操作");
final AsyncHttpResponseHandler resHandler1 = new AsyncHttpResponseHandler(){
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable e) {
//做一些异常处理
e.printStackTrace();
Toast.makeText(BudejieActivity.this, "网络异常", 0).show();
}
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
//数据解析完添加到ListView
//System.out.println("response is :"+new String(responseBody,"utf-8"));
result = new String(responseBody,"utf-8");
readJson(result);
//System.out.println("解析到的数据:"+dataList);
adapter = new MyAdapter(BudejieActivity.this, dataList);
listView.setAdapter(adapter);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}};
new ShowApiRequest( "http://route.showapi.com/255-1", "12672", "fe08f7b8ae3f4e3cb6855304fe96bbeb")
.setResponseHandler(resHandler1)
.addTextPara("page", "1")
.post();
}
if(msg.arg2==2){
System.out.println("加载操作");
final AsyncHttpResponseHandler resHandler2 = new AsyncHttpResponseHandler(){
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable e) {
//做一些异常处理
e.printStackTrace();
Toast.makeText(BudejieActivity.this, "网络异常", 0).show();
}
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
//数据解析完添加到ListView
//System.out.println("response is :"+new String(responseBody,"utf-8"));
result = new String(responseBody,"utf-8");
readJson(result);
//System.out.println("解析到的数据:"+dataList);
adapter = new MyAdapter(BudejieActivity.this, dataList);
listView.setAdapter(adapter);
listView.setSelection(currentList);
currentList = listView.getCount();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}};
new ShowApiRequest( "http://route.showapi.com/255-1", "12672", "fe08f7b8ae3f4e3cb6855304fe96bbeb")
.setResponseHandler(resHandler2)
.addTextPara("page", Integer.toString(currentPage+1))
.post();
currentPage+=1;
}
}
}
}
activity_budejie.xml不得姐页面布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background= "@drawable/bg4"
>
<RelativeLayout
android:id="@+id/layout_title"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="@drawable/title_bg">
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="百思不得姐"
android:textSize="@dimen/activity_main_title_text_size"
android:layout_centerInParent="true"
android:gravity="center"/>
</RelativeLayout>
<com.eric.mylistview.PullToRefreshLayout
android:id="@+id/refresh_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/layout_title" >
<include layout="@layout/layout_head_refresh" />
<com.eric.mylistview.PullableListView
android:id="@+id/content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp" />
<include layout="@layout/layout_head_load" />
</com.eric.mylistview.PullToRefreshLayout>
</RelativeLayout>
layout_listview_item.xml列表子项布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
>
<RelativeLayout
android:id="@+id/layout_budejie_head"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_budejie_touxiang"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/view9"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image_budejie_touxiang"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textSize="10sp"
android:text="EricFantastic"/>
<TextView
android:id="@+id/text_create_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textSize="10sp"
android:text="2015-11-23 16:52"/>
</LinearLayout>
</RelativeLayout>
<TextView
android:id="@+id/text_budejie_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout_budejie_head"
android:textColor="#FFF"
android:textSize="18sp"
android:text="hahahahahahhah"/>
<ImageView
android:id="@+id/image_budejie_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/text_budejie_text"
/>
<WebView
android:id="@+id/WebView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layerType="software"
android:visibility="gone"
android:layout_below="@id/image_budejie_content"/>
<RelativeLayout
android:id="@+id/layout_budejie_end"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/WebView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:background="@drawable/love"/>
<TextView
android:id="@+id/text_love"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textColor="#FFF"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:text="2220"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:background="@drawable/hate"/>
<TextView
android:id="@+id/text_hate"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textColor="#FFF"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:text="235"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
ChatActivity小星问答页面,发送文本到指定API接口,返回响应数据;
ChatActivity.java类似于微信聊天页面,发送用户输入数据给API接口,返回响应数据更新页面。
package com.eric.activity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.eric.constellation.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/*
*@author Eric
*@2015-11-21上午8:21:17
*/
public class ChatActivity extends Activity implements OnClickListener{
public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static String userAgent ="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
//配置您申请的KEY
public static final String APPKEY ="c67cfd82f71f1e011c7c6d40c7e263bb";
private ImageView image_voice,image_plus,image_send,image_chat_back;
private EditText edit_chat;
private Drawable img_userDrawable,img_friendDrawable;
private String sendMsg,getMsg;
public static ChatHandler handler;
private ArrayList<HashMap<String, Object>> chatList = new ArrayList<HashMap<String,Object>>();
String[] from = {"img","text"};
int[] to = {R.id.image_me,R.id.text_me,R.id.image_friend,R.id.text_friend};
int[] item_layout = {R.layout.i_sendmsg_item,R.layout.friend_sendmsg_item};
private ListView listview_chat;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.activity_chat);
image_voice = (ImageView) findViewById(R.id.image_voice);
image_plus = (ImageView) findViewById(R.id.image_plus);
image_send = (ImageView) findViewById(R.id.image_send);
image_chat_back = (ImageView) findViewById(R.id.image_chat_back);
listview_chat = (ListView) findViewById(R.id.listview_chat);
edit_chat = (EditText) findViewById(R.id.edit_chat);
randomDrawable();
image_chat_back.setOnClickListener(this);
image_voice.setOnClickListener(this);
image_plus.setOnClickListener(this);
image_send.setOnClickListener(this);
myAdapter = new MyAdapter(this, chatList, item_layout, from, to);
listview_chat.setAdapter(myAdapter);
listview_chat.setSelection(myAdapter.getCount()-1);
handler = new ChatHandler();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.image_chat_back:
finish();
break;
case R.id.image_send:
sendMessage();
break;
case R.id.image_voice:
break;
case R.id.image_plus:
break;
}
}
public void randomDrawable(){
Random random = new Random();
int n = random.nextInt(3);
Resources resourses = this.getResources();
Resources resourses2 = this.getResources();
switch (n) {
case 0:
img_friendDrawable = resourses.getDrawable(R.drawable.view2);
img_userDrawable = resourses2.getDrawable(R.drawable.view1);
break;
case 1:
img_friendDrawable = resourses.getDrawable(R.drawable.view12);
img_userDrawable = resourses2.getDrawable(R.drawable.view4);
break;
case 2:
img_friendDrawable = resourses.getDrawable(R.drawable.view10);
img_userDrawable = resourses2.getDrawable(R.drawable.view3);
break;
}
}
public void sendMessage(){
sendMsg = edit_chat.getText().toString();
if(sendMsg==null||sendMsg.trim().equals("")){
edit_chat.setError("发送内容不能为空!");
}else{
//这里发送消息
;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
getMsg = getRequest(sendMsg);
Message msg = new Message();
Bundle b = new Bundle();
b.putString("strObject", getMsg);
msg.setData(b);
handler.sendMessage(msg);
}
});
thread.start();
edit_chat.setText(null);
addChatToListview(0, sendMsg);
//将ListView设置为置底显示,自动刷新UI,使得消息及时显示
listview_chat.setSelection(myAdapter.getCount()-1);
}
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit_chat.getWindowToken(),0);
}
//获取机器人数据
public static String getRequest(String info){
String result =null;
String url ="http://op.juhe.cn/robot/index";//请求接口地址
Map<String, Object> params = new HashMap<String, Object>();//请求参数
params.put("key",APPKEY);//您申请到的本接口专用的APPKEY
params.put("info",info);//要发送给机器人的内容,不要超过30个字符
params.put("dtype","json");//返回的数据的格式,json或xml,默认为json
params.put("loc","");//地点,如北京中关村
params.put("lon","");//经度,东经116.234632(小数点后保留6位),需要写为116234632
params.put("lat","");//纬度,北纬40.234632(小数点后保留6位),需要写为40234632
params.put("userid","");//1~32位,此userid针对您自己的每一个用户,用于上下文的关联
try {
result =net(url, params, "GET");
System.out.println("FUCK="+result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//网络请求
public static String net(String strUrl, Map<String, Object> params,String method) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
StringBuffer sb = new StringBuffer();
if(method==null || method.equals("GET")){
strUrl = strUrl+"?"+urlencode(params);
}
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
if(method==null || method.equals("GET")){
conn.setRequestMethod("GET");
}else{
conn.setRequestMethod("POST");
conn.setDoOutput(true);
}
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}
//将map型转为请求参数型
public static String urlencode(Map<String, ?> data) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, ?> i : data.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public class ChatHandler extends Handler{
private Drawable draw;
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
Bundle b = msg.getData();
String strObjcet = b.getString("strObject");
try {
JSONTokener jsonParser = new JSONTokener(getMsg);
JSONObject object = (JSONObject) jsonParser.nextValue();
JSONObject resultObject = object.getJSONObject("result");
String result = resultObject.getString("text");
if(result != null){
addChatToListview(1, result);
}
} catch (JSONException e) {
System.out.println("错误:"+e);
e.printStackTrace();
}
listview_chat.setSelection(myAdapter.getCount()-1);
}
}
public void addChatToListview(int who,String chat){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("who", who);
map.put("chat", chat);
chatList.add(map);
}
public class MyAdapter extends BaseAdapter{
Context context;
ArrayList<HashMap<String,Object>> chatList= null;
int[] layout;
String[] from;
int[] to;
LayoutInflater listcontainer;
public MyAdapter(Context context,
ArrayList<HashMap<String, Object>> chatList, int[] layout,
String[] from, int[] to){
super();
this.context = context;
listcontainer = LayoutInflater.from(context);
this.chatList = chatList;
this.layout = layout;
this.from = from;
this.to = to;
}
@Override
public int getCount() {
return chatList.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int ItemId) {
return ItemId;
}
@Override
public View getView(int item, View view, ViewGroup parent) {
//获取当前子项的who和chat
int who = (Integer)chatList.get(item).get("who");
String msg = (String)chatList.get(item).get("chat");
//根据who设置当前子项的布局和文本,0为自己发送的布局,1为好友发送的布局
if(who==0)
{
view=listcontainer.inflate(R.layout.i_sendmsg_item, null);
((TextView)view.findViewById(R.id.text_me)).setText(msg);
((TextView)view.findViewById(R.id.text_me)).setTextColor(android.graphics.Color.BLUE);
((ImageView)view.findViewById(R.id.image_me)).setImageDrawable(img_userDrawable);
((TextView)view.findViewById(R.id.text_me)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//我发送消息文本点击事件
}
});
((ImageView)view.findViewById(R.id.image_me)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//我头像点击事件
}
});
return view;
}
else
{
view=listcontainer.inflate(R.layout.friend_sendmsg_item, null);
((TextView)view.findViewById(R.id.text_friend)).setText(msg);
((TextView)view.findViewById(R.id.text_friend)).setTextColor(android.graphics.Color.BLUE);
((ImageView)view.findViewById(R.id.image_friend)).setImageDrawable(img_friendDrawable);
((TextView)view.findViewById(R.id.text_friend)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//好友消息文本点击事件
}
});
((ImageView)view.findViewById(R.id.image_friend)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//好友头像点击事件
}
});
return view;
}
}
}
}
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background= "@drawable/bg4"
>
<RelativeLayout
android:id="@+id/layout_title_chat"
android:layout_width="fill_parent"
android:layout_height="@dimen/activity_main_title_height"
android:layout_alignParentTop="true"
android:background="@drawable/title_bg">
<TextView
android:id="@+id/text_title_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="小星问答"
android:textSize="@dimen/activity_main_title_text_size"
android:layout_centerInParent="true"
android:gravity="center"/>
<ImageView
android:id="@+id/image_chat_back"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="@drawable/title_back_up_down"
/>
</RelativeLayout>
<ListView
android:id="@+id/listview_chat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:listSelector="@drawable/chatlist_item_up_down"
android:layout_below="@id/layout_title_chat"
android:divider="#aaaaaa"
android:cacheColorHint="#0000"
android:dividerHeight="0px"
></ListView>
<RelativeLayout
android:id="@+id/layout_chat_bottom"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@drawable/title_bg"
>
<ImageView
android:id="@+id/image_voice"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/voice_up_down"/>
<EditText
android:id="@+id/edit_chat"
android:layout_width="220dp"
android:layout_height="40dp"
android:layout_marginLeft="50dp"
android:layout_centerVertical="true"
android:inputType="text"
android:background="@drawable/title_bg"
android:textColor="#FFF"
/>
<ImageView
android:id="@+id/image_plus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="50dp"
android:background="@drawable/plus_up_down"/>
<ImageView
android:id="@+id/image_send"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="@drawable/send_up_down"/>
</RelativeLayout>
</RelativeLayout>
i_sendmsg_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="50dp"
android:paddingRight="8dp"
android:paddingTop="10dp"
android:paddingBottom="10dp" >
<ImageView
android:id="@+id/image_me"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_alignParentRight="true"
/>
<TextView
android:id="@+id/text_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textSize="18sp"
android:textStyle="bold"
android:layout_toLeftOf="@id/image_me"
android:background="@drawable/i_chat_bg_up_down"
/>
</RelativeLayout>
friend_sendmsg_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="50dp"
android:paddingTop="10dp"
android:paddingBottom="10dp" >
<ImageView
android:id="@+id/image_friend"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/text_friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textSize="18sp"
android:textStyle="bold"
android:layout_toRightOf="@id/image_friend"
android:background="@drawable/friend_chat_up_down"
/>
</RelativeLayout>
ConstellationMatchActivity星座配对页面,自定义滚轮控件,跳转数据页面;
ConstellationSecretActivity星座揭秘页面,简单文本;
ConstellationWallActivity星座壁纸页面,添加自定义动画及轮播图片预览效果;
HelpActivity帮助页面,简单文本;
JokeActivity笑话大全页面,获取笑话数据显示列表,下拉刷新上拉加载;
LuckActivity本日星座运势页面,请求当日星座运势数据;
LuckMonthActivity本月星座运势页面,请求本月星座运势数据;
LuckNextWeekActivity下周星座运势页面,请求下周星座运势数据;
LuckTomoActivity明日星座运势页面;
LuckWeekActivity本周星座运势页面;
LuckYearActivity本年星座运势页面;
MainActivity主界面,标题、fragment容器及tab选项;
StartActivity启动页面,随机图片;
TalkActivity星座物语页面,简单文本;
TestActivity性格测试页面,根据选项返回测试结果;
目前发现这么写,会占用大量的篇幅,下面一段时间会对项目进行拆分,形成链接,方便读者查看。