(我不知道这里是否允许这样做,我是新的)
我在stackoverflow上找到了一个老问题,这个问题没有得到正确的答案,我也遇到了同样的问题。因此,我真的很感激,如果有人可以看看它,非常感谢你!
Spinner won't respond to clicks...even though onItemSelected is called?
更新:
我自己的代码,我想阻止我的onItemSelected()方法在开始时运行,所以我做的就像那个问这个问题的人一样。我试着在我的代码上移动一点,看看它是否会改变任何东西,但没有运气。老实说,我不知道什么是错的,我还是一个初学者,所以我没有。我真的知道该怎么做..
这是我的代码:
public class WidgetConfig extends Activity implements OnItemSelectedListener{
static EditText info;
private static final String[] paths = { "10", "12", "14", "16", "18", "20",
"22", "24", "26", "28", "30", "32", "34", "36", "38", "40", "50", "60"};
private static final String TAG = "MyActivity";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private ArrayList<String> mMyTypes = null;
private ArrayAdapter<String> mMyAdapter = null;
private Spinner mMyTypeSpinner = null;
// hack for spinner
boolean isFirstRunWithSpinner = true;
public WidgetConfig() {
super();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate() started");
setContentView(R.layout.widgetconfig);
context = WidgetConfig.this;
// back button = cancel
setResult(RESULT_CANCELED);
//--------------------------------------------------
mMyTypeSpinner = (Spinner) findViewById(R.id.TxtSizeSP);
mMyTypes = new ArrayList<String>();
mMyTypes.add("Test string");
mMyAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, paths);
mMyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mMyTypeSpinner.setAdapter(mMyAdapter);
// spinner listener
mMyTypeSpinner.setOnItemSelectedListener(this);
} // onCreate finished
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Log.v(TAG, "OnItemselected started");
if( isFirstRunWithSpinner ) { isFirstRunWithSpinner = false; return; }
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
switch (position) {
case 0:
info.setTextSize(10.0f);
views.setFloat(R.id.tvConfigInput, "setTextSize", 10);
Log.v(TAG, "position 0 chosed");
break;
...
break;
case 17:
info.setTextSize(28.0f);
views.setFloat(R.id.tvConfigInput, "setTextSize", 60);
break;
}
//update widget with spinner input
Log.v(TAG, "update size start");
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Log.v(TAG, "update size over");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
...
}
}
答案 0 :(得分:0)
if( isFirstRunWithSpinner ) {
isFirstRunWithSpinner = false;
}else{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
switch (position) {
case 0:
info.setTextSize(10.0f);
views.setFloat(R.id.tvConfigInput, "setTextSize", 10);
Log.v(TAG, "position 0 chosed");
break;
...
break;
case 17:
info.setTextSize(28.0f);
views.setFloat(R.id.tvConfigInput, "setTextSize", 60);
break;
}
//update widget with spinner input
Log.v(TAG, "update size start");
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Log.v(TAG, "update size over");
}