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

如何從獲得listview上item的內容
詳見:https://www.jb51.net/article/158000.htm
中遇到的問題部分。
布局實現:
<?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
//如果多項信息建議用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相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數據庫操作技巧總結》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。