我在将这个XML文件更改为android spinner时遇到了一些问题。我在Android开发中仍然是新手,我希望有人能帮助我。
<channel id="1">
<title>M</title>
<description>my new TV. Viewer: 15+ </description>
<icon>image</icon>
<link>link</link>
答案 0 :(得分:1)
你需要这样做
在strings.xml中定义:
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item two</item>
<item>Array Item Three</item>
</string-array>
在你的布局中:
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="@array/array_name"
/>
答案 1 :(得分:1)
您可以使用Spinner对象向布局添加微调器。您通常应该在XML布局中使用元素执行此操作。例如:
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
如果您的微调器的所需选项是预先确定的,您可以为它们提供在values文件夹下的string.xml资源中定义的字符串数组,如下所示,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
现在,您可以使用适配器将该数组列表分配给微调器对象,如下所示,
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
答案 2 :(得分:0)
打开“res / values / strings.xml”文件,定义将在Spinner(下拉列表)中显示的项目列表。
文件:res / values / strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyAndroidApp</string>
<string name="country_prompt">Choose a country</string>
<string-array name="country_arrays">
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
</resources>
打开“res / layout / main.xml”文件,添加两个微调器组件和一个按钮。
In “spinner1″, the “android:entries” represents the selection items in spinner.
In “spinner2″, the selection items will be defined in code later.
文件:res / layout / main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/country_arrays"
android:prompt="@string/country_prompt" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
3.File:MyAndroidAppActivity.java
package com.mkyong.android;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MyAndroidAppActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyAndroidAppActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}
}
文件:CustomOnItemSelectedListener.java
package com.mkyong.android;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
答案 3 :(得分:0)
首先在值文件夹下的.xml文件中创建字符串数组,如:
<!-- Spinner Adapter Array -->
<string-array name="Month">
<item>JAN</item>
<item>FEB</item>
<item>MAR</item>
<item>APR</item>
<item>MAY</item>
<item>JUN</item>
<item>JUL</item>
<item>AUG</item>
<item>SEP</item>
<item>OCT</item>
<item>NOV</item>
<item>DEC</item>
</string-array>
<!-- END -->
现在在布局文件中创建一个微调器视图,如:
<Spinner
android:id="@+id/months"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="35dp"
android:gravity="center"
android:paddingLeft="10dp"
android:prompt="@string/template_prompt" />
现在在您的activity类中创建一个适配器Like:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(activity, R.array.Month, R.layout.spinner_row);
将此适配器绑定到您的微调器对象之后比如:
yourSpinner.setAdapter(adapter);
哦,您也可以将下拉视图资源设置为适配器,如:
adapter.setDropDownViewResource(R.layout.spinner_textview);
将它绑定到微调器之前。希望它会对你有所帮助。