欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)之ListView的簡單用法及定制ListView界面操作示例

 更新時間:2019年04月03日 12:05:09   作者:水中魚之1999  
這篇文章主要介紹了Android開發(fā)之ListView的簡單用法及定制ListView界面操作,結合實例形式分析了Android ListView界面布局相關操作技巧,需要的朋友可以參考下

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

效果:

如何從獲得listview上item的內容

詳見:http://www.dbjr.com.cn/article/158000.htm

中遇到的問題部分。

布局實現:

  • 有個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">
        <!--存放新的信息-->
        <!--設置最大行數-->
        <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一條數據,所以只用了ArrayAdapter
//如果多項信息建議用BaseAdapter
public class MainActivity extends AppCompatActivity {
  //當前消息列表
  ListView list01 ;
  //消息發(fā)送欄
  EditText editText01 ;
  //消息發(fā)送按鈕
  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,"請輸入后再發(fā)送",Toast.LENGTH_SHORT).show();
        }
      }
    });
  }
}

帶圖片Demo:

Demo下載地址:點擊此處本站下載。

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數據庫操作技巧總結》及《Android資源操作技巧匯總

希望本文所述對大家Android程序設計有所幫助。

相關文章

最新評論