展开全部
重写一下Adapter,然后自己对Adapter每个Item布局,在布局里面设置一下点击效果就行了。如:
public class MainActivity extends Activity {
private Spinner mSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
mSpinner = (Spinner)findViewById(R.id.spinner);
MyAdapter adapter = new MyAdapter(getResources().getStringArray(R.array.girl_names));
mSpinner.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter{
private String[] idInt ;
public MyAdapter(String[] idInt){
this.idInt = idInt;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return idInt.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return idInt[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1 == null){
arg1 = getLayoutInflater().inflate(R.layout.spinner_item, null);
}
TextView text = (TextView)arg1.findViewById(R.id.spinner_item_id);
text.setText(idInt[arg0]);
return arg1;
}
}
spinner_item.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:background="@drawable/text_selector"
android:orientation="vertical" >
<TextView
android:layout_margin="10dip"
android:id="@+id/spinner_item_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true" >
</TextView>
</LinearLayout>
然后:text_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@android:color/black"></item>
<item android:state_pressed="false" android:drawable="@android:color/transparent"></item>
</selector>
public class MainActivity extends Activity {
private Spinner mSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
mSpinner = (Spinner)findViewById(R.id.spinner);
MyAdapter adapter = new MyAdapter(getResources().getStringArray(R.array.girl_names));
mSpinner.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter{
private String[] idInt ;
public MyAdapter(String[] idInt){
this.idInt = idInt;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return idInt.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return idInt[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1 == null){
arg1 = getLayoutInflater().inflate(R.layout.spinner_item, null);
}
TextView text = (TextView)arg1.findViewById(R.id.spinner_item_id);
text.setText(idInt[arg0]);
return arg1;
}
}
spinner_item.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:background="@drawable/text_selector"
android:orientation="vertical" >
<TextView
android:layout_margin="10dip"
android:id="@+id/spinner_item_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true" >
</TextView>
</LinearLayout>
然后:text_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@android:color/black"></item>
<item android:state_pressed="false" android:drawable="@android:color/transparent"></item>
</selector>
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家可靠的代理服务提供商,提供原生IP(住宅原生IP)和高匿名代理服务。以下是关于StormProxies的原生IP服务的一些信息:1. 住宅原生IP:StormProxies提供的住宅原生IP是指从真实的家庭或企...
点击进入详情页
本回答由Storm代理提供
展开全部
1.确定需要的数据放到res/values/arrays.xml文件(定义数组)
-省份-
河北省
山西省
北京市
-城市-
承德市
邯郸市
廊坊市
-城市-
海淀区
朝阳区
崇文区
-城市-
大同市
临汾市
2.编写xml界面res/values/main.xml文件(编写界面)
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/province"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/province"
android:id="@+id/city"
/>
3.在Activity中实现省市联动功能
/**
* 通过Spinner实现省市(两级)联动
* @author 黑色天空
*/
public class MainActivity extends Activity {
private TextView textView ;
private Spinner province;
private Spinner city;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//String[] provinces = new String[]{"-省份-","河北省","山西省","北京"};
textView = (TextView)this.findViewById(R.id.textView);
province = (Spinner)this.findViewById(R.id.province);
city = (Spinner)this.findViewById(R.id.city);
//(处理省的显示)
//将可选内容与ArrayAdapter的连接(从资源数组文件中获取数据)
ArrayAdapter adapter =
ArrayAdapter.createFromResource(this, R.array.province,
android.R.layout.simple_spinner_item);
//new ArrayAdapter(this,android.R.layout.simple_spinner_item,
provinces);
//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将数据绑定到Spinner视图上
province.setAdapter(adapter);
//添加条目被选中监听器
province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView parent, View view,int position, long
id) {
//parent既是province对象
Spinner spinner = (Spinner)parent;
String pro = (String)spinner.getItemAtPosition(position);
//(处理省的市的显示)
//将默认值与ArrayAdapter连接(从资源数组文件中获取数据)
ArrayAdapter cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.citydefault,
android.R.layout.simple_spinner_item);
//new ArrayAdapter
// (MainActivity.this,android.R.layout.simple_spinner_item, cities);
//获取所在省含有哪些市(从资源数组文件中获取数据)
if(pro.equals("河北省")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.hb, android.R.layout.simple_spinner_item);
}else if(pro.equals("北京市")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.bj, android.R.layout.simple_spinner_item);
}else if(pro.equals("山西省")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.shx, android.R.layout.simple_spinner_item);
}
//绑定数据到Spinner(City)上
city.setAdapter(cityAdapter);
}
@Override
public void onNothingSelected(AdapterView parent) {
}
});
}
}
-省份-
河北省
山西省
北京市
-城市-
承德市
邯郸市
廊坊市
-城市-
海淀区
朝阳区
崇文区
-城市-
大同市
临汾市
2.编写xml界面res/values/main.xml文件(编写界面)
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/province"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/province"
android:id="@+id/city"
/>
3.在Activity中实现省市联动功能
/**
* 通过Spinner实现省市(两级)联动
* @author 黑色天空
*/
public class MainActivity extends Activity {
private TextView textView ;
private Spinner province;
private Spinner city;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//String[] provinces = new String[]{"-省份-","河北省","山西省","北京"};
textView = (TextView)this.findViewById(R.id.textView);
province = (Spinner)this.findViewById(R.id.province);
city = (Spinner)this.findViewById(R.id.city);
//(处理省的显示)
//将可选内容与ArrayAdapter的连接(从资源数组文件中获取数据)
ArrayAdapter adapter =
ArrayAdapter.createFromResource(this, R.array.province,
android.R.layout.simple_spinner_item);
//new ArrayAdapter(this,android.R.layout.simple_spinner_item,
provinces);
//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将数据绑定到Spinner视图上
province.setAdapter(adapter);
//添加条目被选中监听器
province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView parent, View view,int position, long
id) {
//parent既是province对象
Spinner spinner = (Spinner)parent;
String pro = (String)spinner.getItemAtPosition(position);
//(处理省的市的显示)
//将默认值与ArrayAdapter连接(从资源数组文件中获取数据)
ArrayAdapter cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.citydefault,
android.R.layout.simple_spinner_item);
//new ArrayAdapter
// (MainActivity.this,android.R.layout.simple_spinner_item, cities);
//获取所在省含有哪些市(从资源数组文件中获取数据)
if(pro.equals("河北省")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.hb, android.R.layout.simple_spinner_item);
}else if(pro.equals("北京市")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.bj, android.R.layout.simple_spinner_item);
}else if(pro.equals("山西省")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.shx, android.R.layout.simple_spinner_item);
}
//绑定数据到Spinner(City)上
city.setAdapter(cityAdapter);
}
@Override
public void onNothingSelected(AdapterView parent) {
}
});
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
今天遇到一个问题:一个Activity(A)跳转到另一个Activity(B),我的想法是从A中ListView选中的项中提取出想要的字符串,然后传递到B中,当B中接收到传递过来的信息后,对B中的一个Spinner做相应的改变。
A传数据给B很简单,在A中用Intent去putExtra就行, 然后到B中的onCreate()中用getExtras就行了,可是怎么才能让Spinner也跟着改变呢?
原来spinner有一个setSelection(),将spinner设为全局变量后用spinner.setSelection(GestureConstant.FILE_GESTURE, false);就可以了。
贴一部分代码,不完整。
A Activity
[java] view plaincopy
Intent fileIntent = new Intent(FileManager.this,
CreateGestureActivity.class);
fileIntent.putExtra("file", "file");
startActivity(fileIntent);
B Activity
[java] view plaincopy
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.create_gesture);
spinner = (Spinner) findViewById(R.id.spinner);
// 设置spinner的adapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.add_gesture_prompt,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setPrompt("创建手势类型");
// 设置spinner的监听器
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
Toast.makeText(CreateGestureActivity.this,
parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
switch (position)
{
// 文件
case GestureConstant.FILE_GESTURE:
textView.setText("路径:");
editText.setText("mnt/sdcard/");
button_choose.setText("添加");
current_gesture = GestureConstant.FILE_GESTURE;
overlay.clear(true);
button_ok.setEnabled(false);
// button.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
getIntent2SetSpinner();
}
/**
* 获取传递过来的Intent信息
*/
private void getIntent2SetSpinner()
{
Intent i = getIntent();
Bundle b = i.getExtras();
if("file".equals(b.getString("file")))
{
Log.i(TAG, "------------------------------------------------------>OK?");
spinner.setSelection(GestureConstant.FILE_GESTURE, false);
editText.setText("mnt/");
}
}
A传数据给B很简单,在A中用Intent去putExtra就行, 然后到B中的onCreate()中用getExtras就行了,可是怎么才能让Spinner也跟着改变呢?
原来spinner有一个setSelection(),将spinner设为全局变量后用spinner.setSelection(GestureConstant.FILE_GESTURE, false);就可以了。
贴一部分代码,不完整。
A Activity
[java] view plaincopy
Intent fileIntent = new Intent(FileManager.this,
CreateGestureActivity.class);
fileIntent.putExtra("file", "file");
startActivity(fileIntent);
B Activity
[java] view plaincopy
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.create_gesture);
spinner = (Spinner) findViewById(R.id.spinner);
// 设置spinner的adapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.add_gesture_prompt,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setPrompt("创建手势类型");
// 设置spinner的监听器
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
Toast.makeText(CreateGestureActivity.this,
parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
switch (position)
{
// 文件
case GestureConstant.FILE_GESTURE:
textView.setText("路径:");
editText.setText("mnt/sdcard/");
button_choose.setText("添加");
current_gesture = GestureConstant.FILE_GESTURE;
overlay.clear(true);
button_ok.setEnabled(false);
// button.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
getIntent2SetSpinner();
}
/**
* 获取传递过来的Intent信息
*/
private void getIntent2SetSpinner()
{
Intent i = getIntent();
Bundle b = i.getExtras();
if("file".equals(b.getString("file")))
{
Log.i(TAG, "------------------------------------------------------>OK?");
spinner.setSelection(GestureConstant.FILE_GESTURE, false);
editText.setText("mnt/");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
您好,您这样:
spinner有一个setSelection(),将spinner设为全局变量后用spinner.setSelection(GestureConstant.FILE_GESTURE, false);就可以了。
贴一部分代码,不完整。
Activity
[java] view plaincopy
Intent fileIntent = new Intent(FileManager.this,
CreateGestureActivity.class);
fileIntent.putExtra("file", "file");
startActivity(fileIntent);
spinner有一个setSelection(),将spinner设为全局变量后用spinner.setSelection(GestureConstant.FILE_GESTURE, false);就可以了。
贴一部分代码,不完整。
Activity
[java] view plaincopy
Intent fileIntent = new Intent(FileManager.this,
CreateGestureActivity.class);
fileIntent.putExtra("file", "file");
startActivity(fileIntent);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询