如何创建下拉列表?我试过ScrollView,但它不是我所需要的。
当前回答
试试这个…
<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) {
}
}
);
}
其他回答
在Kotlin中,您可以这样做:
首先,将这些代码放入布局中
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
然后你可以在onCreate()在活动作为->
val spinner = findViewById<Spinner>(R.id.spinner)
val items = arrayOf("500g", "1kg", "2kg")
val adapter = ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_dropdown_item,
items
)
spinner.setAdapter(adapter)
你可以从下拉菜单中获取listener:
spinner.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
arg0: AdapterView<*>?,
arg1: View?,
arg2: Int,
arg3: Long
) {
// Do what you want
val items = spinner.selectedItem.toString()
}
override fun onNothingSelected(arg0: AdapterView<*>?) {}
}
试试这个:
package example.spin.spinnerexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the bank name list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
activity_main.xml: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
要将你的列表动态添加到旋转器中,就像从webservice中添加数组列表中的项目,并将其加载到旋转器中
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/catNameSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
/>
spinner = findViewById(R.id.catNameSpinner);
ArrayList<String> cat = new ArrayList<>();
cat.add("Choose");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_dropdown_item_1line,cat);
spinner.setAdapter(spinnerAdapter);
试试这个…
<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) {
}
}
);
}
您可以通过以下简单的步骤创建旋转器
首先在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字符串