如何创建下拉列表?我试过ScrollView,但它不是我所需要的。
当前回答
您可以通过以下简单的步骤创建旋转器
首先在XML中创建旋转器
<Spinner
android:id="@+id/select"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#070707"></Spinner>
现在在值中创建字符串arary
<string-array name="itemselect">
<item>Repurchase</item>
<item>Coupons</item>
</string-array>
现在在Java文件中初始化
public class MemberCart_Activity extends AppCompatActivity {
Spinner select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member_cart);
select=findViewById(R.id.select);
ArrayAdapter<String> myadapter=new ArrayAdapter<String>(Main_Activity.this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.itemselect));
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
select.setAdapter(myadapter);
其他回答
你也可以使用AppCompatSpinner小部件:
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spinner_order_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="@color/red"/>
在Activity类内部:
AppCompatSpinner spinOrderType = (AppCompatSpinner) findViewById(R.id.spinner_order_type);
List<String> categories = new ArrayList<String>();
categories.add(getString(R.string.label_table_order));
categories.add(getString(R.string.label_take_away));
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mContext,
R.layout.layout_spinner_item, categories);
dataAdapter.setDropDownViewResource(R.layout.layout_spinner_item);
spinOrderType.setAdapter(dataAdapter);
spinOrderType.setSelection(0);
spinOrderType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
String item = parent.getItemAtPosition(position).toString();
Log.d(TAG, item);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
layout_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="left"
android:textSize="@dimen/text.size.large"
android:textColor="@color/text.link"
android:padding="@dimen/margin.3" />
试试这个…
<string-array name="names">
<item></item>
<item>By Bus</item>
<item>By Train</item>
<item>By Van</item>
<item>By Bike</item>
</string-array>
String travel_type;
ArrayAdapter<String> myAdapter = new ArrayAdapter(AddNew_Trip.this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.names));
myAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
mySpinner.setAdapter(myAdapter);
mySpinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
travel_type = String.valueOf(adapterView.getItemAtPosition(i));
//Toast.makeText(Plan_Trip.this, travel_type, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
);
}
你需要一个旋转器。下面是一个例子:
spinner_1 = (Spinner) findViewById(R.id.spinner1);
spinner_1.setOnItemSelectedListener(this);
List<String> list = new ArrayList<String>();
list.add("RANJITH");
list.add("ARUN");
list.add("JEESMON");
list.add("NISAM");
list.add("SREEJITH");
list.add("SANJAY");
list.add("AKSHY");
list.add("FIROZ");
list.add("RAHUL");
list.add("ARJUN");
list.add("SAVIYO");
list.add("VISHNU");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_1.setAdapter(adapter);
spinner_2 = (Spinner) findViewById(R.id.spinner_two);
spinner_2.setOnItemSelectedListener(this);
List<String> city = new ArrayList<String>();
city.add("KASARGOD");
city.add("KANNUR");
city.add("THRISSUR");
city.add("KOZHIKODE");
city.add("TRIVANDRUM");
city.add("ERNAMKULLAM");
city.add("WAYANAD");
city.add("PALAKKAD");
city.add("ALAPUZHA");
city.add("IDUKKI");
city.add("KOTTAYAM");
city.add("PATHANAMTHITTA");
city.add("KOLLAM");
city.add("MALAPPURAM");
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, city);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_2.setAdapter(adapter2);
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(this, "YOUR SELECTION IS : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
创建Spinner inm XML android:条目= " @array /位置” 然后悬停在数组/位置上并创建资源文件
资源文件应该是这样的 ` “新约克郡”
</resources>`
Then
binding.spinner.selectedItem.toString()
您可以通过以下简单的步骤创建旋转器
首先在XML中创建旋转器
<Spinner
android:id="@+id/select"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#070707"></Spinner>
现在在值中创建字符串arary
<string-array name="itemselect">
<item>Repurchase</item>
<item>Coupons</item>
</string-array>
现在在Java文件中初始化
public class MemberCart_Activity extends AppCompatActivity {
Spinner select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member_cart);
select=findViewById(R.id.select);
ArrayAdapter<String> myadapter=new ArrayAdapter<String>(Main_Activity.this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.itemselect));
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
select.setAdapter(myadapter);
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 数组与列表的性能
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 为什么这个迭代的列表增长代码给出IndexError:列表赋值索引超出范围?如何重复添加(追加)元素到列表?
- 在Python中将列表初始化为特定长度
- 拆分字符串,在一行中转换为ist<int>()
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串