适配器 QQ发送页面

MainActivity

package com.example.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private ArrayList<String> data = null;
	private ArrayAdapter adapter=null;
	private ListView listView;
	private EditText msgEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		data = new ArrayList<String>();

		listView = (ListView) findViewById(R.id.list);
		adapter = new MyAdapter(this, -1);
		
		listView.setAdapter(adapter);

		

		msgEditText = (EditText) findViewById(R.id.msgEditText);

		Button send = (Button) findViewById(R.id.msgSend);
		send.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				sendMessage();

			}
		});

	}

	private void sendMessage() {
		data.add(msgEditText.getText() + "");

		adapter.notifyDataSetChanged();

		// 发送后清空输入框内容
		msgEditText.setText(null);

		// 输入框发送消息后将ListView滚动到最底部
		listView.setSelection(ListView.FOCUS_DOWN);
	}

	private class MyAdapter extends ArrayAdapter {

		private LayoutInflater inflater = null;

		public MyAdapter(Context context, int resource) {
			super(context, resource);

			inflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null)
				convertView = inflater.inflate(R.layout.item, null);

			TextView text = (TextView) convertView.findViewById(R.id.textView1);
			text.setText(data.get(position));

			return convertView;
		}

		@Override
		public int getCount() {
			return data.size();
		}
	}
}


activity_main

<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="@drawable/jjj">
      
     <ListView
          android:id="@+id/list"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_above="@+id/commentLinearLayout"
          android:layout_alignParentTop="true"
          
          android:divider="@android:color/transparent"
          android:dividerHeight="15dip"
          android:scrollbars="none" />

    
      <LinearLayout
        android:id="@+id/commentLinearLayout" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#e0e0e0"
      	android:orientation="horizontal">
          
          <EditText
            android:id="@+id/msgEditText"
            
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:hint="发送消息" />

        <Button
            android:id="@+id/msgSend"
            style="?android:attr/buttonStyleSmall"
            android:background="@drawable/round"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="发送" />

        </LinearLayout>
     
   
</RelativeLayout>


item


<?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"
    
   >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/image"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/image"
        android:background="@drawable/round"
        android:layout_marginRight="5dip"
        android:padding="10dip"
        android:text="没有数据"
        android:textSize="15sp" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle"
        android:layout_alignParentRight="true"
        
        android:src="@drawable/ic_launcher" />
    
</RelativeLayout>


round

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 背景填充颜色值 -->  
    <solid android:color="#2196f3" />  
      
    <!-- radius值越大,越趋于圆形 -->  
    <corners android:radius="20dip" />  
      
    <!-- 圆角图像内部填充四周的大小 ,将会以此挤压内部布置的view -->  
    <padding  
        android:bottom="5dip"  
        android:left="5dip"  
        android:right="5dip"  
        android:top="5dip" />  

</shape>

circle

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    
     android:shape= "oval"  
     android:useLevel= "false" >  
    
    <solid android:color= "#FFA500" />
    
</shape>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值