片段中的 Spinner Adding/Getting 值
Spinner Adding/Getting Value in Fragment
在 Spinner in Fragment 中添加和获取价值的最佳方式是什么?
XML
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Status"
android:textSize="25dp"
android:padding="5dp"/>
Java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View root= inflater.inflate(R.layout.fragment_add, container, false);
Spinner status = root.findViewById(R.id.Status);
return root;`
`
添加数据
String[] role = new String[] {"Faculty","Student"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,role);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
status.setAdapter(adapter);
获取数据
String state = status.getSelectedItem().toString();
你必须使用OnItemSelectedListener
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, id: Long) {
}
override fun onNothingSelected(adapterView: AdapterView<*>?) {
}
}
在 Spinner in Fragment 中添加和获取价值的最佳方式是什么?
XML
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Status"
android:textSize="25dp"
android:padding="5dp"/>
Java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View root= inflater.inflate(R.layout.fragment_add, container, false);
Spinner status = root.findViewById(R.id.Status);
return root;`
`
添加数据
String[] role = new String[] {"Faculty","Student"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,role);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
status.setAdapter(adapter);
获取数据
String state = status.getSelectedItem().toString();
你必须使用OnItemSelectedListener
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, id: Long) {
}
override fun onNothingSelected(adapterView: AdapterView<*>?) {
}
}