0

我想做的很简单,我有一个带有几个项目、edittext 和一个按钮的微调器。我希望能够使用微调器选择某个项目,然后键入某个值来编辑文本,然后单击一个按钮。根据我之前选择的微调器项目,文本视图将在活动中发生变化。

public void submitButtonClick (View submit){
    Spinner s1 = (Spinner)findViewById(R.id.spinner1);
    Button b1 = (Button)findViewById(R.id.button2);

    if (b1.performClick())
    {
        switch (){

        }
    }
}

这是我到目前为止想出的,如果我点击按钮 b1,下面的 switch 语句应该开始(如果选择了第 1 项,做某事等),但我不知道如何实现这一点。如果有人可以提供帮助,我将不胜感激。谢谢

这是我到目前为止所拥有的:

public void submit (View v){
    Button b1 = (Button)findViewById(R.id.button2);
    final Spinner s1 = (Spinner)findViewById(R.id.spinner1);
    final Context context = this;

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int position = s1.getSelectedItemPosition();

            switch (position){
            case 0:
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                alertDialogBuilder.setTitle("Warning");
                alertDialogBuilder.setMessage("Please choose an item from the list");
                alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        Bifrost.this.finish();
                    }
                });
                AlertDialog spinnerError = alertDialogBuilder.create();
                spinnerError.show();
                break;
            case 1:
                break;
            }
        }
    });
}

代码没有给出错误,应用程序正常启动,但是当我选择第一个项目然后单击按钮时没有任何反应。我在创建对话框时做错了吗?

4

4 回答 4

1

You first need to set an onClickListener to your button, and in this you need to get the selected item of the spinner.

b1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        int position = s1.getSelectedItemPosition();

         switch(position){
              case 0: //first item
              break;
              case 1: //second item
              break;
         }
    }
});
于 2013-06-22T09:42:19.497 回答
0

You can do something like this. you need to set a onClickListener to the button. when the button is clicked then the onClick method will be called. At that method check the selected item of the spinner.. Forexmple

    Spinner s1 = (Spinner)findViewById(R.id.spinner1);
    Button b1 = (Button)findViewById(R.id.button2);

   b1.setOnclickListener(new onClickListener(){
         public void onCLick(View v){

            switch(s1.getSelectedItemPosition()){
                case 0:
                   // do something 
                   `enter code here`break
                 ..........
            }
         }  
      }
   );

I just tried to give a conceptual idea the code is not exact , but this is how you should do it. So my suggestion is before implementing this leanr the basic features of buttons, spinners, onCLickListeners.

于 2013-06-22T09:42:02.733 回答
0

您可以为您的微调器分配一个侦听器。

s1.setOnItemSelectedListener(new OnItemSelectedListener() {

    // will run every time the user select an item from your spinner
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

        // change your textView here, base on position (index of item selected in spinner)
        if (position == 0) {

            // user selected the first item in spinner

        } 

        else if (position == 1) {

            // user selected the second item in spinner

        }

        // and so on...
    }


});

之后,您可以为您的按钮分配另一个侦听器(与上面的代码类似 - 匿名内部类、匿名声明和初始化)。它还必须覆盖 onClick 等。网上有很多关于这一切的资源。

希望这可以帮助!

于 2013-06-22T09:37:17.860 回答
0

您可以使用以下方法之一来获取微调器的选定项。

spinner.getSelectedItem() 
spinner.getSelectedItemPosition()

你使用哪一个,与你如何将物品加载到你的微调器中有关。请点击此链接以获取有关这些方法的更多信息。

于 2013-06-22T09:38:19.360 回答