代码在Main活动中正常运行。如何将此微调器类扩展到项目中的所有活动?
只有在XML中添加微调器条目时,微调器的项才可见 而且,没有调用onItemSelected。我已经按照每个教程进行了操作但是无法弄清楚如何做到这一点。
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="@array/MenuItems" />
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="@array/MenuItems" />
没有以编程方式添加项目。
public class MainMenu extends Activity implements OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.MenuItems));
spinner.setAdapter(spinnerAdapter);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String item = adapterView.getItemAtPosition(i).toString();
// Showing selected spinner item
Toast.makeText(adapterView.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
我的activity_main如下
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="com.example.supreetkaur.pocbeacon.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:background="?attr/colorPrimary" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="@array/MenuItems" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
我是Android开发的初学者。真的很感激任何帮助。