(我不知道这是否允许,我是新来的)

我在这里在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) {

}

    ...

}


}

最佳答案

你尝试过这个吗? :

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");
}

10-04 12:05
查看更多