我希望spinner
下拉列表在页面加载时自动打开。
我该怎么办?
<Spinner
android:entries="@array/cities_arrays"
android:id="@+id/spinner"
android:layout_width="230sp"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:spinnerMode="dropdown"
tools:layout_editor_absoluteY="16dp"
android:background="@drawable/spinner_styling"
android:layout_marginLeft="80sp"
android:layout_marginRight="80sp"
android:layout_marginTop="10sp"
android:layout_below="@+id/app_bar"/>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
placesList = new ArrayList<>();
cities = getResources().getStringArray(R.array.cities_arrays);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, cities);
listView = (ListView) findViewById(R.id.listView);
// This is to avoid automatic keypad opening as soon as the page loads.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
placesList.clear();
autoCompView = (AutoCompleteTextView) findViewById(R.id.autoCompleteText);
autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item));
autoCompView.setOnItemClickListener(this);
listAdapter = new ListViewAdapter(this, R.layout.row,placesList);
spinner.setAdapter(adapter);
// Open the Spinner when your activity will load...
spinner.performClick();
mButton = (Button) findViewById(R.id.plus_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// inflate alert dialog xml
LayoutInflater li = LayoutInflater.from(mContext);
View dialogView = li.inflate(R.layout.custom_dialog, null);
android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(mContext);
// set title
alertDialogBuilder.setTitle("Add Location");
// set custom dialog icon
//alertDialogBuilder.setIcon(R.drawable.ic_launcher);
// set custom_dialog.xml to alertdialog builder
alertDialogBuilder.setView(dialogView);
final EditText userInput = (EditText) dialogView
.findViewById(R.id.autoCompleteTextView);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
//etOutput.setText(userInput.getText());
dialog.cancel();
}
})
.setNegativeButton("Done",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
android.support.v7.app.AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
index = arg0.getSelectedItemPosition();
city = cities[index];
if(index != 0) {
PLACES_OF_INTEREST_URL = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" + city + "+point+of+interest&language=en&key=" + API_KEY + "";
listView.clearAnimation();
listAdapter.clear();
new GetPlaces().execute();
listAdapter.addAll(placesList);
listView = (ListView) findViewById(R.id.listView);
listView.invalidateViews();
listAdapter.notifyDataSetChanged();
listView.setAdapter(listAdapter);
System.out.println(city);
Toast.makeText(getApplicationContext(), "Selected: " + city, Toast.LENGTH_LONG).show();
}
index = -1;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getApplicationContext(),"Please Select a City!", Toast.LENGTH_LONG).show();
}
});
}
E / AndroidRuntime:致命异常:主要 处理:com.application.microsoft.wayfarer,PID:24758 java.lang.RuntimeException:无法启动活动ComponentInfo {com.application.microsoft.wayfarer / com.application.microsoft.wayfarer.activities.MainActivity}:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null为无效;你的活动在运行吗?
答案 0 :(得分:0)
您可以像下面这样实现: -
Spinner yourSpinner = (Spinner) findViewById(R.id.yourSpinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.planets, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourSpinner.setAdapter(adapter);
// Open the Spinner when your activity will load...
yourSpinner.performClick();
并在您的方法中注释以下行
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);