spinner组件类似于html中的select标签,实现下拉选择框的功能。
添加一个Activity,界面上添加一个spinner下拉框,一个button按钮。点击按钮,获取spinner下拉框当前选择的内容
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
tools:context=".MainActivity"> <Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="60dp" /> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取内容"
app:layout_constraintTop_toBottomOf="@+id/spinner" />
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package com.example.chenrui.app1; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast; import java.lang.reflect.Array; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final Spinner spinner = findViewById(R.id.spinner);
String[] data ={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,data);
spinner.setAdapter(adapter); Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "您选择了:" + spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
下拉框的列表内容,放在一个数组中。或者可以把这个列表内容放到资源文件中,放到资源文件的代码如下,注意红色文字部分为修改内容:
package com.example.chenrui.app1; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast; import java.lang.reflect.Array; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final Spinner spinner = findViewById(R.id.spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.week_array,R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter); Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "您选择了:" + spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
上面的R.array.week_array资源是保存在res/values/strings.xml资源文件中,内容如下:
<resources>
<string name="app_name">app1</string> <string-array name="week_array">
<item>星期一</item>
<item>星期二</item>
<item>星期三</item>
<item>星期四</item>
<item>星期五</item>
<item>星期六</item>
<item>星期日</item>
</string-array>
</resources>
运行的效果如下: