首页 > 系统 > Android > 正文

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

2019-12-12 00:14:35
字体:
来源:转载
供稿:网友

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

效果:

如何从获得listview上item的内容

详见:https://www.VeVB.COm/article/158000.htm

中遇到的问题部分。

布局实现:

  • 有个listview显示
  • 一个edit和button发送
<?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>

添加方法:

//此处由于只有String一条数据,所以只用了ArrayAdapter//如果多项信息建议用BaseAdapterpublic 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相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表