首頁 > 軟體

Android Spinner和GridView元件的使用範例

2022-03-31 13:03:27

一. 概述:

        Spinner是我們所熟悉的下拉框列表。與ListView類似,我們必須為Spinner物件指定一個Adapter。我們從Spinner的簡單用法開始講述。

二. 實現

MainActivity.java

package com.example.demo03_29;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    TextView choice;
    ArrayAdapter<CharSequence> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        choice=(TextView) this.findViewById(R.id.choice);
 
        Spinner spinner=(Spinner)this.findViewById(R.id.sp1);
        adapter=ArrayAdapter.createFromResource(this, R.array.habit,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }
 
 
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        choice.setText(adapter.getItem(i));
    }
 
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
 
    }
}

在values包下的  string.xml

<resources>
    <string name="app_name">Demo03-29</string>
<string-array name="habit">
    <item>10公里夜跑</item>
    <item>爬山</item>
    <item>游泳</item>
    <item>吃飯</item>
    <item>打程式碼</item>
</string-array>
</resources>

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你喜歡的運動"/>
        <Spinner
            android:id="@+id/sp1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
 
    <View
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#05ADF8"/>
 
    <TextView
        android:id="@+id/choice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
</LinearLayout>

sipnner_down.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
 
    <ImageView
        android:id="@+id/p01"
        android:layout_width="64dp"
        android:layout_height="64dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/t2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </LinearLayout>
 
</LinearLayout>

         這個佈局比較簡單,就是在LinearLayout中放置一個LinearLayout、一個分隔元件用的View和一個顯示所選結果的TextView。在其中巢狀的LinearLayout中放置一個用於提示用的TextView和下拉框Spinner。

三. 美化

MyAdapter.java 

package com.example.demo03_29;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import androidx.annotation.NonNull;
 
public class MyArrayApater extends ArrayAdapter {
    private LayoutInflater minflater;
    String[] titles;
    String[] desc={
            "整合工具",
            "高速儲存,隨時儲存",
            "優質資源,盡在迅雷",
            "整合開發工具",
            "學習神器"
    };
    int[] photos={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5};
 
    public MyArrayApater(@NonNull Context context, int resource, CharSequence[] objects) {
        super(context, resource, objects);
 
        minflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        titles=context.getResources().getStringArray(R.array.habit);
    }
    @Override
    public int getCount(){
        return titles.length;
    }
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent){
        View v;
        v=minflater.inflate(R.layout.spinner_down,parent,false);
 
        ImageView iv=(ImageView)v.findViewById(R.id.p01);
        iv.setImageResource(photos[position]);
        TextView tv01=(TextView) v.findViewById(R.id.t1);
        tv01.setText(titles[position]);
        TextView tv02=(TextView) v.findViewById(R.id.t2);
        tv02.setText(desc[position]);
        return v;
    }
}

修改 MainActivity.java

四. GridView

       GridView以二維表格的方式顯示資料,若資料比較多,該元件將提供垂直卷軸。我們用一個列子來說明GridView的使用方法。

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
 
    <GridView
        android:id="@+id/gr1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="4"
        />
 
</LinearLayout>

 ImgeAdapter.java

package com.example.demo0330;
 
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
 
public class ImageAdapter extends BaseAdapter {
    private int[] images={
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
 
    };
    private Context context;
    public ImageAdapter(Context context){
        this.context=context;
    }
 
 
    @Override
    public int getCount() {
        return images.length;
    }
 
    @Override
    public Object getItem(int position) {
        return images[position];
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView==null){
            imageView=new ImageView(context);
            int width= GridView.LayoutParams.MATCH_PARENT;
            int heght= GridView.LayoutParams.MATCH_PARENT;
            imageView.setLayoutParams(new ViewGroup.LayoutParams(width,heght));
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        }else{
            imageView=(ImageView) convertView;
        }
        imageView.setImageResource(images[position]);
        return imageView;
    }
}

MainActivity.java

package com.example.demo0330;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        GridView gridView=(GridView) findViewById(R.id.gr1);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Toast.makeText(MainActivity.this,"現在點選的是位置在"+(position+1)+"張照片",Toast.LENGTH_LONG).show();
            }
        });
    }
}

效果圖:

到此這篇關於Android Spinner和GridView元件的使用範例的文章就介紹到這了,更多相關Android Spinner內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com