1

我无法在微调器中选择项目。当我单击微调器中的项目时,Logcat 显示警告

W/InputManagerService(60):窗口已经聚焦,忽略焦点增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@406f3d90

代码示例

package com.example.myspinner;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.Spinner;
import android.widget.ArrayAdapter;

public class MainActivity extends Activity {

    private static class RootGroup extends ViewGroup {

        Spinner spinner;

        public RootGroup(Context context) {
            super(context);

            spinner = new Spinner(context);
            addView(spinner);

            String[] objects = {"Item 1", "Item 2", "Item 3", "Item 4"};
            ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context, android.R.layout.simple_spinner_item, objects);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            if (changed) {
                int height = b - t; 
                int width = r - l;
                int widthSpec = MeasureSpec.makeMeasureSpec(r - l, MeasureSpec.EXACTLY);
                int heightSpec = MeasureSpec.makeMeasureSpec(height / 8, MeasureSpec.EXACTLY);
                spinner.measure(widthSpec, heightSpec);
                spinner.layout(0, 0, width / 2, height / 4);                
            }
        }
    }    


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ViewGroup rootGroup = new RootGroup(MainActivity.this);
        setContentView(rootGroup);
    }
}

你能帮助我吗?

4

1 回答 1

0

我没有看到您在代码中为微调器设置侦听器:

            spinner
                    .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        public void onItemSelected(AdapterView<?> arg0,
                                View v, int position, long id) {


                        }

                        public void onNothingSelected(AdapterView<?> arg0) {

                        }
                    });
于 2013-11-07T14:30:51.103 回答