如何创建下拉列表?我试过ScrollView,但它不是我所需要的。
当前回答
parseInt(binding.inputAge.text.toString()),
新的android资源文件(dropdown_item)在R.layout。Dropdown_item,把textview放在里面。
TextView代码:
`<TextView android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14sp"
android:text="textView"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold"
xmlns:android="http://schemas.android.com/apk/res/android"/>`
//绑定适配器并获取值 binding.ddCity.setAdapter (showCity) binding.ddCity.selectedItem.toString ()
其他回答
转轮xml:
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
java:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Spinner spinner;
private static final String[] paths = {"item 1", "item 2", "item 3"};
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
// Whatever you want to happen when the first item gets selected
break;
case 1:
// Whatever you want to happen when the second item gets selected
break;
case 2:
// Whatever you want to happen when the thrid item gets selected
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
你也可以使用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" />
这个代码对我来说很好,希望它也能帮助到你。
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dip" >
<TextView
android:id="@+id/spinnerItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="12sp" >
</TextView>
</RelativeLayout>
details.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="20dp"
android:background="#FF00FF">
<Spinner
android:id="@+id/dropStatus"
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_marginBottom="7dp"
android:drawSelectorOnTop="true"/>
</LinearLayout>
适配器类:
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class StatusAdapter extends ArrayAdapter<StatusBean> {
private Context context;
private ArrayList<StatusBean> statuses;
public Resources res;
StatusBean currRowVal = null;
LayoutInflater inflater;
public StatusAdapter(Context context,
int textViewResourceId, ArrayList<StatusBean> statuses,
Resources resLocal) {
super(context, textViewResourceId, statuses);
this.context = context;
this.statuses = statuses;
this.res = resLocal;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.status_item, parent, false);
currRowVal = null;
currRowVal = (StatusBean) statuses.get(position);
TextView label = (TextView) row.findViewById(R.id.spinnerItem);
if (position == 0) {
label.setText("Please select status");
} else {
label.setText(currRowVal.getStatus());
}
return row;
}
}
StatusBean class:
public class StatusBean {
private String status;
private String statusCode;
public StatusBean() {
}
public StatusBean(String status,
String statusCode) {
this.status = status;
this.statusCode = statusCode;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
}
活动班级:
在onCreate方法内部:
static ArrayList<StatusBean> STATUS_LIST = new ArrayList<StatusBean>();
for(int i=0;i<=10;i++) {
STATUS_LIST.add(new StatusBean(“Status ”+i, “Stattus ”+i));
}
final Spinner dropStatus = (Spinner)findViewById(R.id.dropStatus);
Resources res = getResources();
StatusAdapter adapter = new StatusAdapter(this, R.layout.item, SessionData. STATUS_LIST, res);
dropStatus.setAdapter(adapter);
创建Spinner inm XML android:条目= " @array /位置” 然后悬停在数组/位置上并创建资源文件
资源文件应该是这样的 ` “新约克郡”
</resources>`
Then
binding.spinner.selectedItem.toString()
在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<*>?) {}
}
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在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字符串