Android開發(fā)之ListView的簡(jiǎn)單用法及定制ListView界面操作示例
本文實(shí)例講述了Android開發(fā)之ListView的簡(jiǎn)單用法及定制ListView界面操作。分享給大家供大家參考,具體如下:
效果:

如何從獲得listview上item的內(nèi)容
詳見:http://www.dbjr.com.cn/article/158000.htm
中遇到的問題部分。
布局實(shí)現(xiàn):
- 有個(gè)listview顯示
- 一個(gè)edit和button發(fā)送
<?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>
<!--用于存放和發(fā)送新的信息-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:background="#ffffff">
<!--存放新的信息-->
<!--設(shè)置最大行數(shù)-->
<EditText
android:id="@+id/ifo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入內(nèi)容"
android:textColorHint="#c0c0c0"
android:maxLines="6"/>
<!--點(diǎn)擊發(fā)送消息-->
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發(fā)送"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
添加方法:
//此處由于只有String一條數(shù)據(jù),所以只用了ArrayAdapter
//如果多項(xiàng)信息建議用BaseAdapter
public class MainActivity extends AppCompatActivity {
//當(dāng)前消息列表
ListView list01 ;
//消息發(fā)送欄
EditText editText01 ;
//消息發(fā)送按鈕
Button button01_send ;
//記錄數(shù)組長(zhǎng)度
int arr_num = 0;
//定義一個(gè)數(shù)組
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,"請(qǐng)輸入后再發(fā)送",Toast.LENGTH_SHORT).show();
}
}
});
}
}
帶圖片Demo:

Demo下載地址:點(diǎn)擊此處本站下載。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android百度地圖應(yīng)用之MapFragment的使用
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用之MapFragment的使用的相關(guān)資料,需要的朋友可以參考下2016-06-06
Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā))
本篇文章主要介紹了Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā)),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Android獲取手機(jī)聯(lián)系人電話號(hào)碼并返回結(jié)果
這篇文章主要為大家詳細(xì)介紹了Android獲取手機(jī)聯(lián)系人電話號(hào)碼并返回結(jié)果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
AndroidStduio3.0 使用gradle將module打包jar文件的方法
這篇文章主要介紹了AndroidStduio3.0 使用gradle將module打包jar文件的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04

