本文实例讲述了Android开发之ListView的简单用法及定制ListView界面操作。分享给大家供大家参考,具体如下:
效果:
如何从获得listview上item的内容
详见:http://www.tuohang.net/article/130811.html
中遇到的问题部分。
布局实现:
- 有个listview显示
- 一个edit和button发送
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:orientation = "vertical" > <!--使用红色得分割条--> < ListView android:id = "@+id/list1" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:divider = "#f00" android:dividerHeight = "2px" android:headerDividersEnabled = "false" > </ ListView > <!--用于存放和发送新的信息--> < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_alignParentBottom = "true" android:orientation = "vertical" android:background = "#ffffff" > <!--存放新的信息--> <!--设置最大行数--> < EditText android:id = "@+id/ifo" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:hint = "请输入内容" android:textColorHint = "#c0c0c0" android:maxLines = "6" /> <!--点击发送消息--> < Button android:id = "@+id/send" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "发送" android:textSize = "16sp" /> </ LinearLayout > </ RelativeLayout > |
添加方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
//此处由于只有String一条数据,所以只用了ArrayAdapter //如果多项信息建议用BaseAdapter public class MainActivity extends AppCompatActivity { //当前消息列表 ListView list01 ; //消息发送栏 EditText editText01 ; //消息发送按钮 Button button01_send ; //记录数组长度 int arr_num = 0 ; //定义一个数组 String[] arr1 = new String[arr_num]; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); list01 = (ListView) findViewById(R.id.list1); editText01 = (EditText) findViewById(R.id.ifo); button01_send = (Button) findViewById(R.id.send); button01_send.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if ( ! editText01.getText().toString().equals( "" ) ){ String[] arr_new = new String[++arr_num]; // System.arraycopy(arr1,0,arr_new,0, arr1.length); for ( int j = 0 ; j < arr1.length; j++){ arr_new[j] = arr1[j]; } arr_new[arr_num- 1 ] = editText01.getText().toString(); arr1 = arr_new; ArrayAdapter adapter1; adapter1 = new ArrayAdapter<>(MainActivity. this ,R.layout.array_list,arr_new); list01.setAdapter(adapter1); editText01.setText( "" ); } else { Toast.makeText(MainActivity. this , "请输入后再发送" ,Toast.LENGTH_SHORT).show(); } } }); } } |
带图片Demo:
Demo下载地址:点击此处本站下载。
希望本文所述对大家Android程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_43377749/article/details/84109916