Spinner使用限制及扩展

本文介绍了一种解决Spinner控件默认选中项时触发事件的方法。通过扩展Spinner并重写performClick方法,实现在再次选中同一项时也能触发相应事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spinner使用限制:
当Spinner下拉框默认选中某一项时,当再选中这一项时,setOnItemSelectedListener事件就无法进入此事件,当想处理在选中这一项时,再想去处理一个动作,这样就无法实现
Spinner使用扩展
为了解决上面的问题可以使用performClick方法
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Spinner android:id="@+id/Spinner01" android:layout_width="wrap_content" android:layout_height="wrap_content">
</Spinner>
<RelativeLayout android:id="@+id/Relative"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom = "true"
        android:visibility="visible">
</RelativeLayout>       
</LinearLayout>
spinner.java
package com.spinner;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
public class spinner extends Activity {
    /** Called when the activity is first created. */
       
        private static final String[] m_arr = {"第一组","第二组","第三组"};
        private int selectedIndex = 0;
        private int cloneselectedIndex = 0;
        private Spinner s2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Spinner s1 = (Spinner) findViewById(R.id.Spinner01);
        s1.setPrompt("请选择" );
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_arr);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
        s1.setAdapter(adapter);  
        s1.setSelection(0,true);
        
        s1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
                dispToast("选择的是"+m_arr[arg2]);
                arg0.setVisibility(View.VISIBLE);
            }
            public void onNothingSelected(AdapterView<?> arg0){
            }
        });
        
        ArrayAdapter<String> mAdapter_ = new ArrayAdapter<String>(this,
                                android.R.layout.simple_spinner_item, m_arr);
        RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.Relative);
        s2 = new Spinner(this){
                public boolean performClick() {
                                new AlertDialog.Builder(spinner.this).setTitle("请选择")
                                        .setSingleChoiceItems(m_arr, selectedIndex,
                                                new DialogInterface.OnClickListener() {
                                                        public void onClick(DialogInterface dialog,
                                                                        int whichButton) {
                                                                cloneselectedIndex = whichButton; // 记下哪一个选项被选择
                                                        }
                                                }).setPositiveButton("确定",
                                                        new DialogInterface.OnClickListener() {
                                                                public void onClick(DialogInterface dialog,
                                                                                int whichButton) {
                                                                        selectedIndex = cloneselectedIndex;
                                                                        s2.setSelection(selectedIndex);
                                                                        dispToast("选择的是"+m_arr[selectedIndex]);
                                                                }
                                                        }).setNegativeButton("取消",
                                                                new DialogInterface.OnClickListener() {
                                                                        public void onClick(DialogInterface dialog,
                                                                                        int whichButton) {
                       
                                                                        }
                                                                }).create().show();
                                return true;
                }
        };
        s2.setAdapter(mAdapter_);
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.ALIGN_BOTTOM);
                rlayout.addView(s2);
     }
   
     public void dispToast(String str){
         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
     }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.spinner"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".spinner"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>
spinner1.bmp
2010-12-23 12:05:34 上传
下载附件 (685.29 KB)

spinner2.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值