如何创建下拉列表?我试过ScrollView,但它不是我所需要的。
当前回答
创建Spinner inm XML android:条目= " @array /位置” 然后悬停在数组/位置上并创建资源文件
资源文件应该是这样的 ` “新约克郡”
</resources>`
Then
binding.spinner.selectedItem.toString()
其他回答
这个代码对我来说很好,希望它也能帮助到你。
。
<?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);
你也可以使用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" />
创建Spinner inm XML android:条目= " @array /位置” 然后悬停在数组/位置上并创建资源文件
资源文件应该是这样的 ` “新约克郡”
</resources>`
Then
binding.spinner.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
}
}
简单的/优雅的/我怎么做的:
预览:
XML:
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
spinnerMode设置为dropdown是android的下拉方式。(https://developer.android.com/reference/android/widget/Spinner attr_android: spinnerMode)
Java:
//get the spinner from the xml.
Spinner dropdown = findViewById(R.id.spinner1);
//create a list of items for the spinner.
String[] items = new String[]{"1", "2", "three"};
//create an adapter to describe how the items are displayed, adapters are used in several places in android.
//There are multiple variations of this, but this is the basic variant.
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
//set the spinners adapter to the previously created one.
dropdown.setAdapter(adapter);
文档:
这是基础知识,但还有更多的东西可以通过实验自学。 https://developer.android.com/guide/topics/ui/controls/spinner.html
你可以使用setOnItemSelectedListener。(https://developer.android.com/guide/topics/ui/controls/spinner.html # SelectListener) 您可以从xml中添加字符串列表。(https://developer.android.com/guide/topics/ui/controls/spinner.html #填充) 这个视图有一个appCompat版本。(https://developer.android.com/reference/androidx/appcompat/widget/AppCompatSpinner)
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在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字符串