2

(我不知道这里是否允许,我是新人)

我在stackoverflow上发现了一个老问题,这个问题没有得到正确答案,我也面临同样的问题。因此,我真的很感激,如果有人可以看看它,非常感谢!

Spinner 不会响应点击...即使 onItemSelected 被调用?

更新:

我自己的代码,好吧,我想阻止我的 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) {

}

    ...

}

}

4

1 回答 1

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");
}
于 2012-11-19T17:02:54.833 回答