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

如何從獲得listview上item的內容
詳見:http://www.dhdzp.com/article/158000.htm
中遇到的問題部分。
布局實現(xiàn):
- 有個listview顯示
- 一個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ù)-->
<EditText
android:id="@+id/ifo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入內容"
android:textColorHint="#c0c0c0"
android:maxLines="6"/>
<!--點擊發(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
//如果多項信息建議用BaseAdapter
public class MainActivity extends AppCompatActivity {
//當前消息列表
ListView list01 ;
//消息發(fā)送欄
EditText editText01 ;
//消息發(fā)送按鈕
Button button01_send ;
//記錄數(shù)組長度
int arr_num = 0;
//定義一個數(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,"請輸入后再發(fā)送",Toast.LENGTH_SHORT).show();
}
}
});
}
}
帶圖片Demo:

Demo下載地址:點擊此處本站下載。
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android使用viewpager實現(xiàn)畫廊式效果
這篇文章主要為大家詳細介紹了Android使用viewpager實現(xiàn)畫廊式效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08
Android中的HTextView庫實現(xiàn)TextView動畫效果
HTextView是一個用來給TextView里的文字做各種轉換動畫的開源庫,不僅提供了多種動畫選擇,而且還有重復字符的位移動畫,雖然并沒有多么復雜,但是它使用的這些典型的設計模式以及各種動畫的實現(xiàn)確實可以從中讓我們學到不少知識2023-12-12
Android實現(xiàn)底部對話框BottomDialog彈出實例代碼
本篇文章主要介紹了Android實現(xiàn)底部對話框BottomDialog代碼。這里整理了詳細的代碼,有需要的小伙伴可以參考下。2017-03-03
淺談Android手機聯(lián)系人開發(fā)之增刪查改功能
這篇文章主要介紹了Android手機聯(lián)系人開發(fā)之增刪查改功能,需要的朋友可以參考下2017-05-05
Android EditText實現(xiàn)分割輸入內容
這篇文章主要為大家詳細介紹了Android EditText實現(xiàn)分割輸入內容的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Android開發(fā) 旋轉屏幕導致Activity重建解決方法
Android開發(fā)文檔上專門有一小節(jié)解釋這個問題。簡單來說,Activity是負責與用戶交互的最主要機制,接下來為您詳細介紹2012-11-11

