服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android开发之ListView的简单用法及定制ListView界面操作示例

Android开发之ListView的简单用法及定制ListView界面操作示例

2022-10-11 15:48水中鱼之1999 Android

这篇文章主要介绍了Android开发之ListView的简单用法及定制ListView界面操作,结合实例形式分析了Android ListView界面布局相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android开发之ListView的简单用法及定制ListView界面操作。分享给大家供大家参考,具体如下:

效果:

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:

Android开发之ListView的简单用法及定制ListView界面操作示例

Demo下载地址:点击此处本站下载

希望本文所述对大家Android程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_43377749/article/details/84109916

延伸 · 阅读

精彩推荐