如何创建下拉列表?我试过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 ()

其他回答

创建Spinner inm XML android:条目= " @array /位置” 然后悬停在数组/位置上并创建资源文件

资源文件应该是这样的 ` “新约克郡”

    </resources>`

Then

binding.spinner.selectedItem.toString()       

要将你的列表动态添加到旋转器中,就像从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);

您可以通过以下简单的步骤创建旋转器

首先在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);

转轮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
        }

}

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 ()