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>
2010-12-23 12:05:34 上传
下载附件 (685.29 KB)
当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>

2010-12-23 12:05:34 上传
下载附件 (685.29 KB)
