看到很多即时通讯软件有聊天功能,今天就试着做一下这个功能,当然这只是模拟下界面,界面效果如下所示:
这里的功能简化为:在输入框输入关键字,随机回复既定的语句,显示在listview中,如果不包含关键字,则回复无法识别。
1,编写界面布局文件acitivity_main.xml,listview + 输入框
<RelativeLayout 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=".MainActivity" > <RelativeLayout android:id="@+id/main_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/bottom_bar" > <EditText android:id="@+id/et_input_content" android:layout_width="300dp" android:layout_height="wrap_content" android:background="@drawable/btn_bg" android:layout_centerVertical="true" android:hint="请输入内容"/> <Button android:id="@+id/btn_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_toRightOf="@id/et_input_content" android:layout_marginLeft="10dp" android:background="@drawable/btn_bg" android:onClick="input" android:text="确定" /> </RelativeLayout> <ListView android:id="@+id/main_list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/main_bottom" > </ListView> </RelativeLayout>
2,listview每个item的布局文件,item_conversation.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 问的部分 --> <TextView android:id="@+id/item_ask_tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:background="@drawable/asker_bubble" android:layout_marginRight="10dp" android:gravity="center" android:text="问的内容" android:textColor="#FFFFFF" /> <!-- 答的部分 --> <LinearLayout android:id="@+id/item_answer_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/answer_bubble" android:layout_marginLeft="10dp" android:orientation="vertical" > <TextView android:id="@+id/item_answer_tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="答的内容" android:textColor="#FFFFFF" /> <ImageView android:id="@+id/item_answer_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/p10" /> </LinearLayout> </LinearLayout>3,编写对话实体,ConversationBean.java
public class ConversationBean { public boolean isAsk; //是否是问 public String askContent; //问的内容 public String answerContent; //回答的内容 public int answerPicId; //回答的图片资源 }
4,回答模拟数据
/** * 临时数据 */ public class TmpData { public static String[] textDatas = new String[] { "点我啊!", "戳我啊!", "我为自己带盐!", "专业办证30年,品质如一", "天王盖地虎", "小鸡炖蘑菇", "色狼,你的最爱", "喜欢吗?", "还要吗", "就这么多了" }; public static int[] imgDatas = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, R.drawable.p10 }; }
5,主程序MainActivity.java,listview绑定数据,每输入一句话,判断是否包含特定字符,随机生成响应的回答,回答模拟数据没有从网络获取,而是直接从本地获取更新数据
public class MainActivity extends Activity { private EditText et_input_content; private Button btn_input; private ListView main_list_view; private MyAdapter mAdapter; private List<ConversationBean> mDatas = new ArrayList<ConversationBean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_input_content = (EditText) findViewById(R.id.et_input_content); btn_input = (Button) findViewById(R.id.btn_input); main_list_view = (ListView) findViewById(R.id.main_list_view); mAdapter = new MyAdapter(); main_list_view.setAdapter(mAdapter); final Random rdm = new Random(); btn_input.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String inputStr = et_input_content.getText().toString().trim(); //更新问的内容 ConversationBean askBean = new ConversationBean(); askBean.isAsk = true; askBean.askContent = inputStr; mDatas.add(askBean); mAdapter.notifyDataSetChanged(); //更新回答的数据 ConversationBean answerBean = new ConversationBean(); if(inputStr.contains("美女")||inputStr.contains("美人")||inputStr.contains("性感")){ answerBean.isAsk = false; answerBean.answerContent = TmpData.textDatas[rdm.nextInt(TmpData.textDatas.length)]; answerBean.answerPicId = TmpData.imgDatas[rdm.nextInt(TmpData.imgDatas.length)]; }else{ answerBean.isAsk = false; answerBean.answerContent = "无法识别"; answerBean.answerPicId = 0; } mDatas.add(answerBean); mAdapter.notifyDataSetChanged(); //滚动到最后一行的位置 main_list_view.smoothScrollToPosition(mAdapter.getCount()); } }); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null){ convertView = View.inflate(MainActivity.this,R.layout.item_conversation,null); holder = new ViewHolder(); convertView.setTag(holder); holder.item_ask_tv_content = (TextView) convertView.findViewById(R.id.item_ask_tv_content); holder.item_answer_container = (LinearLayout) convertView.findViewById(R.id.item_answer_container); holder.item_answer_tv_content = (TextView) convertView.findViewById(R.id.item_answer_tv_content); holder.item_answer_iv = (ImageView) convertView.findViewById(R.id.item_answer_iv); }else{ holder = (ViewHolder) convertView.getTag(); } //设置数据 //判断是问的内容还是回答内容 ConversationBean conversationBean = mDatas.get(position); if(conversationBean.isAsk){ //显示问的内容 holder.item_ask_tv_content.setVisibility(View.VISIBLE); holder.item_answer_container.setVisibility(View.GONE); //设置问的内容 holder.item_ask_tv_content.setText(conversationBean.askContent); }else{ //显示回答的内容 holder.item_ask_tv_content.setVisibility(View.GONE); holder.item_answer_container.setVisibility(View.VISIBLE); holder.item_answer_tv_content.setText(conversationBean.answerContent); if(conversationBean.answerPicId == 0){ holder.item_answer_iv.setVisibility(View.GONE); }else { holder.item_answer_iv.setVisibility(View.VISIBLE); holder.item_answer_iv.setImageResource(conversationBean.answerPicId); } } return convertView; } } class ViewHolder { TextView item_ask_tv_content; LinearLayout item_answer_container; TextView item_answer_tv_content; ImageView item_answer_iv; } }
至此,基本功能已实现,如有疑问欢迎留言或加群讨论:196615382,如需源码,点击下载。。。