瘋狂 Android 講義, 4/e – 列表式選單(ListView & ArrayAdapter & AdapterView & Toast)範例 P103~P111

瘋狂 Android 講義, 4/e – 列表式選單(ListView & ArrayAdapter & AdapterView & Toast)範例 P103~P111

瘋狂 Android 講義, 4/e – 列表式選單(ListView & ArrayAdapter & AdapterView & Toast)範例 P103~P111



資料來源:

    https://github.com/daichangya/book/tree/master/android
    https://pan.baidu.com/s/1d_xYJI0UQ_1tQzSj_V_NIg 提取码:70ch
    https://learnexp.tw/%E3%80%90android%E3%80%91listview-%E6%95%99%E5%AD%B8-%E5%BE%9E-4%E5%88%B05/



XML Code

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/TextView"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"	
	android:textSize="24sp"
	android:checkMark="@drawable/ok"
	android:shadowColor="#f0f"
	android:shadowDx="4"
	android:shadowDy="4"
	android:shadowRadius="2"/>
<?xml version="1.0" encoding="utf-8"?>
<TextView
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/TextView"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"	
	android:textSize="24dp"
	android:padding="5dp"
	android:shadowColor="#f0f"
	android:shadowDx="4"
	android:shadowDy="4"
	android:shadowRadius="2"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<!-- 设置使用红色的分隔条 -->
	<ListView
		android:id="@+id/list1"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:divider="#f00"
		android:dividerHeight="1dp"
		android:headerDividersEnabled="false"/>
	<!-- 设置使用绿色的分隔条 -->
	<ListView
		android:id="@+id/list2"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:divider="#0f0"
		android:dividerHeight="1dp"
		android:headerDividersEnabled="false"/>
</LinearLayout>



Java Code

package com.example.as_test01;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import android.widget.Toast;
import android.widget.AdapterView;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    // 定义一个数组
    public String[] arr1 = new String[]{"孙悟空", "猪八戒", "牛魔王"};

    // 定义一个数组
    public String[] arr2 = new String[]{"Java", "Hibernate", "Spring", "Android"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list1 = findViewById(R.id.list1);
        // 将数组包装为ArrayAdapter
        ArrayAdapter adapter1 = new ArrayAdapter(this, R.layout.array_item, arr1);
        // 为ListView设置Adapter
        list1.setAdapter(adapter1);
        list1.setOnItemClickListener(onClickListView01);       //指定事件 Method

        ListView list2 = findViewById(R.id.list2);
        // 将数组包装为ArrayAdapter
        ArrayAdapter adapter2 = new ArrayAdapter(this, R.layout.checked_item, arr2);
        // 为ListView设置Adapter
        list2.setAdapter(adapter2);
        list2.setOnItemClickListener(onClickListView02);       //指定事件 Method
    }

    /***
     * 點擊ListView事件Method
     */
    private AdapterView.OnItemClickListener onClickListView01 = new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Toast 快顯功能 第三個參數 Toast.LENGTH_SHORT 2秒  LENGTH_LONG 5秒
            Toast.makeText(MainActivity.this,"點選第 "+(position +1) +" 個 \n" + " 內容:" +arr1[position], Toast.LENGTH_SHORT).show();
        }
    };

    /***
     * 點擊ListView事件Method
     */
    private AdapterView.OnItemClickListener onClickListView02 = new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Toast 快顯功能 第三個參數 Toast.LENGTH_SHORT 2秒  LENGTH_LONG 5秒
            Toast.makeText(MainActivity.this,"點選第 "+(position +1) +" 個 \n" + " 內容:" +arr2[position], Toast.LENGTH_SHORT).show();
        }
    };
}

One thought on “瘋狂 Android 講義, 4/e – 列表式選單(ListView & ArrayAdapter & AdapterView & Toast)範例 P103~P111

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *