Android 微调器在 运行 应用程序时不可点击或工作
Android Spinner is not clickable or working while running app
我对编程比较陌生,最近开始使用 android。我整天都在为我遇到的这个问题绞尽脑汁。我阅读了几个类似的问题并找到了几个解决方案和示例,但其中 none 对我有用:(
package com.deitel.welcome;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
public class Preferences extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.preferences, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_profile) {
Intent intent = new Intent(Preferences.this, Profile.class);
startActivity(intent);
}
else if (id == R.id.action_home) {
Intent intent = new Intent(Preferences.this, HomeScreen.class);
startActivity(intent);
}
else if (id == R.id.action_calendar) {
Intent intent = new Intent(Preferences.this, CalendarActivity.class);
startActivity(intent);
}
else if (id == R.id.action_statistics) {
Intent intent = new Intent(Preferences.this, Statistics.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public class MainActivity extends Activity {
private String[] states;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
states = getResources().getStringArray(R.array.countries_list);
spinner = (Spinner) findViewById(R.id.country_spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
}
这是我的 customOnItemSelected java 文件:
package com.deitel.welcome;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"On Item Select : \n" + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
这是我的资源文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="spinner_prompt">Choose a country</string>
<string-array name="country_arrays">
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
</resources>
这当然是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/country_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/country_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/country_label" />
</LinearLayout>
谁能找出我做错了什么?
您好!罗莎
您的这段代码:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
在没有任何逻辑的情况下添加侦听器,因为您没有提供任何内部重写方法。你应该做的是像这样添加你的自定义监听器:
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener())
您在所选项目中未执行任何操作。尝试在所选项目的主要 activity 中添加祝酒消息,为什么在可以使用默认侦听器时需要创建自定义侦听器?
在您的主 activity 中尝试这样做:
spinner.setOnItemSelectedListener(新的 OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(parent.getContext(),"spinner clicked",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
只需尝试在您的默认侦听器中添加祝酒词,或者设置您拥有的自定义侦听器 made.You 没有将其设置为您的侦听器,因此什么也没有发生。
简单 Spinner
示例和 Onclick listner
String[] tracks_time = { "3min", "5min", "10min" };
Spinner spinner_time = (Spinner)promptsView.findViewById(R.id.sp_time);
ArrayAdapter<String> sptime = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, tracks_time);
spinner_time.setAdapter(sptime);
spinner_time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position ,long arg3)
{
int index = parent.getSelectedItemPosition();
// get stuff as per index
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
谢谢大家!你是对的...我发现我实际上在我的 activity 中创建了另一个 class 而在原来的 activity 中什么也没做。谢谢你的帮助!
我将所有内容都复制到了第一个 class 中并添加了一些内容。这是最终对我有用的代码:
public class Preferences extends Activity {
protected CharSequence[] _options = { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
protected boolean[] _selections = new boolean[_options.length];
protected Button _optionsButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
_optionsButton = (Button) findViewById(R.id.button1);
_optionsButton.setOnClickListener(new ButtonClickHandler());
}
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
showDialog(0);
}
}
@Override
protected Dialog onCreateDialog (int id) {
return new AlertDialog.Builder(this)
.setTitle("Preferred Exercise Days")
.setMultiChoiceItems(_options, _selections,
new DialogSelectionClickHandler())
.setPositiveButton("OK", new DialogButtonClickHandler())
.create();
}
}
public class DialogSelectionClickHandler implements
DialogInterface.OnMultiChoiceClickListener {
public void onClick(DialogInterface dialog, int clicked,
boolean selected) {
Log.i("ME", _options[clicked] + " selected: " + selected);
}
}
public class DialogButtonClickHandler implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int clicked) {
switch (clicked) {
case DialogInterface.BUTTON_POSITIVE:
String selected = "";
int i = 0;
for (boolean b : _selections) {
if (b) {
selected += _options[i] + " ;";
}
i++;
}
printSelectedDays();
Toast.makeText(Preferences.this, "Selected: " + selected,
Toast.LENGTH_SHORT).show();
break;
}
}
}
protected void printSelectedDays() {
for (int i = 0; i < _options.length; i++) {
Log.i("ME", _options[i] + " selected: " + _selections[i]);
}
}
解决方案有点不同,但这实际上是我首先想要实现的,所以我希望毕竟有人可以使用它。
我对编程比较陌生,最近开始使用 android。我整天都在为我遇到的这个问题绞尽脑汁。我阅读了几个类似的问题并找到了几个解决方案和示例,但其中 none 对我有用:(
package com.deitel.welcome;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
public class Preferences extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.preferences, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_profile) {
Intent intent = new Intent(Preferences.this, Profile.class);
startActivity(intent);
}
else if (id == R.id.action_home) {
Intent intent = new Intent(Preferences.this, HomeScreen.class);
startActivity(intent);
}
else if (id == R.id.action_calendar) {
Intent intent = new Intent(Preferences.this, CalendarActivity.class);
startActivity(intent);
}
else if (id == R.id.action_statistics) {
Intent intent = new Intent(Preferences.this, Statistics.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public class MainActivity extends Activity {
private String[] states;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
states = getResources().getStringArray(R.array.countries_list);
spinner = (Spinner) findViewById(R.id.country_spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
}
这是我的 customOnItemSelected java 文件:
package com.deitel.welcome;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"On Item Select : \n" + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
这是我的资源文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="spinner_prompt">Choose a country</string>
<string-array name="country_arrays">
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
</resources>
这当然是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/country_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/country_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/country_label" />
</LinearLayout>
谁能找出我做错了什么?
您好!罗莎
您的这段代码:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
在没有任何逻辑的情况下添加侦听器,因为您没有提供任何内部重写方法。你应该做的是像这样添加你的自定义监听器:
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener())
您在所选项目中未执行任何操作。尝试在所选项目的主要 activity 中添加祝酒消息,为什么在可以使用默认侦听器时需要创建自定义侦听器?
在您的主 activity 中尝试这样做:
spinner.setOnItemSelectedListener(新的 OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(parent.getContext(),"spinner clicked",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
只需尝试在您的默认侦听器中添加祝酒词,或者设置您拥有的自定义侦听器 made.You 没有将其设置为您的侦听器,因此什么也没有发生。
简单 Spinner
示例和 Onclick listner
String[] tracks_time = { "3min", "5min", "10min" };
Spinner spinner_time = (Spinner)promptsView.findViewById(R.id.sp_time);
ArrayAdapter<String> sptime = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, tracks_time);
spinner_time.setAdapter(sptime);
spinner_time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position ,long arg3)
{
int index = parent.getSelectedItemPosition();
// get stuff as per index
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
谢谢大家!你是对的...我发现我实际上在我的 activity 中创建了另一个 class 而在原来的 activity 中什么也没做。谢谢你的帮助!
我将所有内容都复制到了第一个 class 中并添加了一些内容。这是最终对我有用的代码:
public class Preferences extends Activity {
protected CharSequence[] _options = { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
protected boolean[] _selections = new boolean[_options.length];
protected Button _optionsButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
_optionsButton = (Button) findViewById(R.id.button1);
_optionsButton.setOnClickListener(new ButtonClickHandler());
}
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
showDialog(0);
}
}
@Override
protected Dialog onCreateDialog (int id) {
return new AlertDialog.Builder(this)
.setTitle("Preferred Exercise Days")
.setMultiChoiceItems(_options, _selections,
new DialogSelectionClickHandler())
.setPositiveButton("OK", new DialogButtonClickHandler())
.create();
}
}
public class DialogSelectionClickHandler implements
DialogInterface.OnMultiChoiceClickListener {
public void onClick(DialogInterface dialog, int clicked,
boolean selected) {
Log.i("ME", _options[clicked] + " selected: " + selected);
}
}
public class DialogButtonClickHandler implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int clicked) {
switch (clicked) {
case DialogInterface.BUTTON_POSITIVE:
String selected = "";
int i = 0;
for (boolean b : _selections) {
if (b) {
selected += _options[i] + " ;";
}
i++;
}
printSelectedDays();
Toast.makeText(Preferences.this, "Selected: " + selected,
Toast.LENGTH_SHORT).show();
break;
}
}
}
protected void printSelectedDays() {
for (int i = 0; i < _options.length; i++) {
Log.i("ME", _options[i] + " selected: " + _selections[i]);
}
}
解决方案有点不同,但这实际上是我首先想要实现的,所以我希望毕竟有人可以使用它。