spinner就相当于Html中的下拉列表框,在下面的例子里我们共同学习一下spinner的使用。
属性:
属性名称 | 描述 |
android:prompt | 该提示在下拉列表对话框显示时显示。(译者注:对话框的标题: |
方法:
public CharSequence getPrompt()
返回值
当对话框弹出的时候显示的提示(获得弹出视图上的标题字)
public void onClick(DialogInterface dialog, int which)
当点击弹出框中的项时这个方法将被调用。
参数
dialog 点击弹出的对话框
which 点击按钮(如:Button)或者点击位置
public Boolean performClick()
如果它被定义就调用此视图的OnClickListener(译者注:例如可以在加载时默认弹出下拉列表)。
返回值
为True一个指定的OnClickListener被调用,为false时不被调用。
public void setOnItemClickListener(AdapterView.OnItemClickListener I)
Spinner不支持item的点击事件,调用此方法将引发异常。
参数
l 这个监听将被忽略
public void setPromptId(CharSequence prompt)
设置对话框弹出的时候显示的提示(译者注:设置弹出视图上的标题字)
参数
prompt 设置的提示
public void setPromptId(int promptId)
设置对话框弹出的时候显示的提示(译者注:设置弹出视图上的标题字)
参数
prompted 当对话框显示是显示这个资源id所代表的提示。
范例:
package android.imageview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class ImageView1Activity extends Activity {
/** Called when the activity is first created. */
private Spinner spinner = null;
private ArrayAdapter<String> adapter = null;
private List<String> list = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner)findViewById(R.id.spinner);
spinner.setPrompt("请选择你喜欢的水果?");
list = new ArrayList<String>();
list.add("苹果");
list.add("香蕉");
list.add("梨");
list.add("桃");
adapter = new ArrayAdapter<String>(ImageView1Activity.this, android.R.layout.simple_dropdown_item_1line,list);
spinner.setAdapter(adapter);
}
}
另一种方法:
package android.imageview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class ImageView1Activity extends Activity {
/** Called when the activity is first created. */
private Spinner spinner1 = null;
private ArrayAdapter<CharSequence> adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner1 = (Spinner)findViewById(R.id.spinner1);
spinner1.setPrompt("你要以后在哪工作?");
adapter = ArrayAdapter.createFromResource(this,R.array.city,android.R.layout.simple_spinner_item);
spinner1.setAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ImageView1Activity!</string>
<string name="app_name">ImageView1</string>
<string-array name = "city">
<item>山东</item>
<item>上海</item>
<item>北京</item>
<item>大连</item>
</string-array>
</resources>
加载进事件:
package android.imageview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class ImageView1Activity extends Activity {
/** Called when the activity is first created. */
private Spinner spinner1 = null;
private ArrayAdapter<CharSequence> adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner1 = (Spinner)findViewById(R.id.spinner1);
spinner1.setPrompt("你要以后在哪工作?");
adapter = ArrayAdapter.createFromResource(this,R.array.city,android.R.layout.simple_spinner_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(ImageView1Activity.this,"选择的城市是: " +spinner1.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}