以编程方式创建微调器点击工作

时间:2014-03-13 22:01:37

标签: java android

我正在以编程方式创建微调器旋转显示完美的下拉列表但是当我单击ant项目时没有显示Toast消息,当我调试时没有进入微调器点击方法我的代码有什么问题? spinner项目会显示但是click事件不起作用有什么问题?

public class MainActivity extends Activity {
Spinner spinner;

String[] strings = { "Power Options", "Power off", "Restart" };

String[] subs = { "", "Shut phone down", "Close all apps and restart phone" };

int arr_images[] = { R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

       spinner = new Spinner(this);
    spinner.setAdapter(new MyAdapter(this, R.layout.row, strings));

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
   {

        public void onItemSelected(AdapterView<?> adapterView, View view,
                int i, long l) {
            // TODO Auto-generated method stub

            if (i == 1) {

    Toast.makeText(MainActivity.this, "PowerOff Perform",
                        Toast.LENGTH_SHORT).show();
            }

            if (i == 2) {

                Toast.makeText(MainActivity.this, "Reboot Perform",
                        Toast.LENGTH_SHORT).show();
            }
        }

        // If no option selected
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });
}

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {

        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView,
            ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.company);
        label.setText(strings[position]);

        TextView sub = (TextView) row.findViewById(R.id.sub);
        sub.setText(subs[position]);

        ImageView icon = (ImageView) row.findViewById(R.id.image);
        icon.setImageResource(arr_images[position]);

        return row;
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (KeyEvent.KEYCODE_BACK == event.getKeyCode()) {

        spinner.performClick();

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

1 个答案:

答案 0 :(得分:0)

可能使用setOnItemClickListener而不是setOnItemSelectedListener?

尝试使用

spinner.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> arg0, final View arg1, final int arg2, final long arg3) {
        }
    });

而不是

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
   {

        public void onItemSelected(AdapterView<?> adapterView, View view,
                int i, long l) {...