forked from hzuapps/android-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
187 additions
and
54 deletions.
There are no files selected for viewing
156 changes: 133 additions & 23 deletions
156
...in/java/edu/hzuapps/androidworks/homeworks/com1314080901128/Com1314080901128Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,151 @@ | ||
package edu.hzuapps.androidworks.homeworks.com1314080901128; | ||
package com.edu.hzu.liaotian; | ||
|
||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
|
||
import android.os.Bundle; | ||
import android.support.v7.app.ActionBarActivity; | ||
import android.os.Handler; | ||
import android.app.Activity; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.ImageView; | ||
import android.widget.ListView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import edu.hzuapps.androidworks.R; | ||
import edu.hzuapps.androidworks.homeworks.BackActivity; | ||
import com.edu.hzu.liaotian.R; | ||
|
||
public class Com1314080901128ctivity extends BackActivity { | ||
public class MainActivity extends Activity implements OnClickListener{ | ||
EditText et_tony_message; | ||
Button but_tony_send; | ||
|
||
EditText et_hill_message; | ||
Button but_hill_send; | ||
|
||
ListView lv_message; | ||
List<Message> list; | ||
MessageAdapter adapter; | ||
Handler handler=new Handler(); | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_com1314080901128); | ||
|
||
Button closeBtn = (Button) findViewById(R.id.button_close); | ||
closeBtn.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
finish(); | ||
} | ||
}); | ||
setContentView(R.layout.activity_main); | ||
init(); | ||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
private void init() { | ||
et_tony_message=(EditText) findViewById(R.id.et_tony_message); | ||
but_tony_send=(Button) findViewById(R.id.but_tony_send); | ||
but_tony_send.setOnClickListener(this); | ||
|
||
et_hill_message=(EditText) findViewById(R.id.et_hill_message); | ||
but_hill_send=(Button) findViewById(R.id.but_hill_send); | ||
but_hill_send.setOnClickListener(this); | ||
|
||
list=new ArrayList<Message>(); | ||
lv_message=(ListView) findViewById(R.id.lv_message); | ||
adapter=new MessageAdapter(); | ||
lv_message.setAdapter(adapter); | ||
} | ||
|
||
|
||
@Override | ||
protected void onPause() { | ||
super.onPause(); | ||
public void onClick(View v) { | ||
|
||
if(v==but_tony_send){ | ||
|
||
if(et_tony_message.getText()==null||et_tony_message.getText().toString().equals("")){ | ||
Toast.makeText(this, "",Toast.LENGTH_LONG).show(); | ||
return ; | ||
} | ||
Message m=new Message(); | ||
m.setFrom_username("Tony"); | ||
m.setCreate_time(System.currentTimeMillis()); | ||
m.setText(et_tony_message.getText().toString()); | ||
sendMessage(m); | ||
et_tony_message.setText(""); | ||
} | ||
|
||
if(v==but_hill_send){ | ||
|
||
if(et_hill_message.getText()==null||et_hill_message.getText().toString().equals("")){ | ||
Toast.makeText(this, "", Toast.LENGTH_LONG).show(); | ||
return ; | ||
} | ||
Message m=new Message(); | ||
m.setFrom_username("Hill"); | ||
m.setCreate_time(System.currentTimeMillis()); | ||
m.setText(et_hill_message.getText().toString()); | ||
sendMessage(m); | ||
et_hill_message.setText(""); | ||
} | ||
} | ||
private void sendMessage(Message m) { | ||
list.add(m); | ||
adapter.notifyDataSetChanged(); | ||
// lv_message.f | ||
lv_message.setSelection(list.size()+1); | ||
} | ||
class MessageAdapter extends BaseAdapter{ | ||
|
||
@Override | ||
protected void onStop() { | ||
super.onStop(); | ||
@Override | ||
public int getCount() { | ||
return list.size(); | ||
} | ||
|
||
@Override | ||
public Object getItem(int position) { | ||
return list.get(position); | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
Message message=list.get(position); | ||
ViewHolder viewHolder=null; | ||
// if(convertView==null){ | ||
if("Tony".equalsIgnoreCase(message.getFrom_username())){ | ||
convertView=parent.inflate(MainActivity.this, R.layout.list_message_item_left, null); | ||
}else{ | ||
convertView=parent.inflate(MainActivity.this, R.layout.list_message_item_right, null); | ||
} | ||
viewHolder=new ViewHolder(); | ||
viewHolder.iv_userhead=(ImageView) convertView.findViewById(R.id.iv_userhead); | ||
viewHolder.tv_chatcontent=(TextView) convertView.findViewById(R.id.tv_chatcontent); | ||
viewHolder.tv_sendtime=(TextView) convertView.findViewById(R.id.tv_sendtime); | ||
viewHolder.tv_username=(TextView) convertView.findViewById(R.id.tv_username); | ||
// convertView.setTag(viewHolder); | ||
// }else{ | ||
// viewHolder=(ViewHolder) convertView.getTag(); | ||
// } | ||
|
||
viewHolder.tv_chatcontent.setText(message.getText()); | ||
viewHolder.tv_sendtime.setText(new SimpleDateFormat("HH:mm:ss").format(new Date(message.getCreate_time()))); | ||
viewHolder.tv_username.setText(message.getFrom_username()); | ||
return convertView; | ||
} | ||
class ViewHolder{ | ||
public ImageView iv_userhead; | ||
public TextView tv_username; | ||
public TextView tv_chatcontent; | ||
public TextView tv_sendtime; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,55 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
android:padding="6dp"> | ||
|
||
<RelativeLayout | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<ImageView | ||
android:id="@+id/iv_userhead" | ||
android:layout_width="40dp" | ||
android:layout_height="40dp" | ||
android:background="@drawable/com1314080901128image_t" | ||
android:focusable="false" | ||
android:layout_alignParentLeft="true" | ||
android:layout_alignParentTop="true" /> | ||
|
||
|
||
<TextView | ||
android:id="@+id/tv_chatcontent" | ||
android:layout_toRightOf="@id/iv_userhead" | ||
android:layout_marginLeft="10dp" | ||
android:layout_marginRight="40dp" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:background="@drawable/com1314080901128images_b"/> | ||
</RelativeLayout> | ||
|
||
</LinearLayout> | ||
<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" | ||
android:background="#ffffff" | ||
tools:context=".MainActivity" > | ||
<ListView | ||
android:id="@+id/lv_message" | ||
android:layout_height="fill_parent" | ||
android:layout_width="fill_parent" | ||
android:layout_above="@+id/ll_send_1" | ||
android:scrollbars="none" | ||
android:divider="@null" | ||
android:footerDividersEnabled="false"> | ||
</ListView> | ||
<LinearLayout | ||
android:id="@+id/ll_send_1" | ||
android:layout_height="50dp" | ||
android:layout_width="fill_parent" | ||
android:orientation="horizontal" | ||
android:layout_above="@+id/ll_send_2"> | ||
<EditText | ||
android:id="@+id/et_tony_message" | ||
android:layout_width="0dp" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="3" | ||
android:hint="Tony请输入"/> | ||
<Button | ||
android:id="@+id/but_tony_send" | ||
android:layout_height="fill_parent" | ||
android:layout_width="0dp" | ||
android:layout_weight="1" | ||
android:text="send"/> | ||
</LinearLayout> | ||
<LinearLayout | ||
android:id="@+id/ll_send_2" | ||
android:layout_height="50dp" | ||
android:layout_width="fill_parent" | ||
android:orientation="horizontal" | ||
android:layout_alignParentBottom="true"> | ||
<EditText | ||
android:id="@+id/et_hill_message" | ||
android:layout_width="0dp" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="3" | ||
android:hint="Hill请输入"/> | ||
<Button | ||
android:id="@+id/but_hill_send" | ||
android:layout_height="fill_parent" | ||
android:layout_width="0dp" | ||
android:layout_weight="1" | ||
android:text="send"/> | ||
</LinearLayout> | ||
</RelativeLayout> |