如何使用微调器

时间:2010-03-08 03:16:15

标签: android spinner

我对android很新。我想在我的应用程序中使用2个微调器,一个显示国家/地区列表,当选择任何国家/地区时,其他微调器应显示该国家/地区的城市列表。选择城市时,会执行某些操作。 PLZ帮我一些示例代码。在期待中感谢

4 个答案:

答案 0 :(得分:10)

以下是我们可以使用的方法将spinner2 w.r.t的选项添加到微调器1中。

public class Activity extends Activity implements View.OnClickListener
{
    private Spinner spinner0, spinner1, spinner2, spinner3;
    private Button submit, cancel;

    private String country[], state[], city[], area[];

    Australia aus = new Australia();

    Object object;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinner0 = (Spinner)findViewById(R.id.spinnerCountry);
        spinner1 = (Spinner)findViewById(R.id.spinnerQ1);
        spinner2 = (Spinner)findViewById(R.id.spinnerQ2);
        spinner3 = (Spinner)findViewById(R.id.spinnerQ3);

        submit = (Button)findViewById(R.id.btnSubmit);
        cancel = (Button)findViewById(R.id.btnCancel);

        submit.setOnClickListener(this);
        cancel.setOnClickListener(this);

        country = new String[] {"Select Country", "Australia", "USA", "UK", "New Zealand", "EU", "Europe", "China", "Hong Kong",
                                "India", "Malaysia", "Canada", "International", "Asia", "Africa"};


        ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, country);
        adapter0.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner0.setAdapter(adapter0);
        Log.i("AAA","spinner0");

        spinner0.setOnItemSelectedListener(new OnItemSelectedListener()
        {           
            @Override
            public void onItemSelected(AdapterView<?> arg0, View view1, int pos, long id)
            {
                Log.i("AAA","OnItemSelected");
                int loc;
                loc = pos;

                switch (loc)
                {
                case 1:

                    state = aus.getState();
                    object = aus;

                    Log.i("AAA","ArrayAdapter1");                   
                    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, state);
                    adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                    spinner1.setAdapter(adapter1);          Log.i("AAA","spinner1");
                    break;

                default:
                    Log.i("AAA","default 0");
                    break;
                }                               
            }       

            @Override
            public void onNothingSelected(AdapterView<?> arg1)
            {
                Log.i("AAA","Nothing S0");

            }
        });

       spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
       {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View view1, int pos, long id) 
            {
                Log.i("AAA","OnItemSelected S1");
                int loc = pos;
                switch(loc)
                {
                    case 1:
                        Log.i("AAA","Australia");
                        if(object.equals(aus))
                        {
                            city = aus.getType(loc);
                        }
                        else
                        {
                            break;
                        }


                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, city);
                        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                        spinner2.setAdapter(adapter);           Log.i("AAA","spinner2");
                        break;

                    default:
                            Log.i("AAA", "default 1");
                        break;

                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                Log.i("AAA","Nothing S1");              
            }

        }); 

       spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
       {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id)
        {
            int loc = pos;
            switch (loc)
            {
            case 1:
                if(object.equals(aus))
                {
                    area = aus.getTitle(loc);
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, area);
                adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                spinner3.setAdapter(adapter);           Log.i("","spinner3");               
                break;

            default:
                break;
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) 
        {
            // TODO Auto-generated method stub          
        }
       });

    }// on-create


    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        case R.id.btnSubmit:

            break;

        case R.id.btnCancel:
            finish();
            break;

        default:
            break;
        }       
    }   
}

如果您觉得这很有用,那么请放弃投票,以便其他人可以轻松找到一个好的答案。

对于每个国家/地区,您必须为其创建一个类,只需添加State,City&amp;区域。这样它就不会成为单页的网格。

玩得开心。

此致

HAPS。

答案 1 :(得分:3)

下面是一个示例代码,它描述了在选择微调器项目时执行的微调器和操作的用法

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

    Spinner spin;
    String spin_val;
    String[] gender = { "Male", "Female" };//array of strings used to populate the spinner
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//setting layout
        spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id
        //Register a callback to be invoked when an item in this AdapterView has been selected
        spin.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) { 
                // TODO Auto-generated method stub
                spin_val = gender[position];//saving the value selected


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });
      //setting array adaptors to spinners 
      //ArrayAdapter is a BaseAdapter that is backed by an array of arbitrary objects
        ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender);

        // setting adapteers to spinners
        spin.setAdapter(spin_adapter);
    }


}

要向微调器添加值列表,您需要在Activity中指定一个SpinnerAdapter,它扩展了Adapter类。微调器适配器允许定义两个不同的视图:一个显示微调器本身的数据和当按下微调器时,显示下拉列表中的数据的那个。你也可以下载&amp;请参阅完整的spinner_demo示例项目以便更好地理解。

答案 2 :(得分:2)

答案 3 :(得分:0)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner)findViewById(R.id.spinner);
    list = new ArrayList<String>();
    list.add("Select your Favorite Color");
    list.add("Blue");
    list.add("White");
    list.add("Red");
    list.add("Green");
    list.add("Black");
    list.add("Yellow");
    list.add("Pink");
    list.add("Orange");
    SpinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           View v = super.getView(position, convertView, parent);
            // set the textColor you want
            return v;
        }

        @Override
        public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View v = super.getDropDownView(position, convertView, parent);
// Set the BackgroundColor
            return v;
        }
    };
    SpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(SpinnerAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String state = adapterView.getItemAtPosition(i).toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

}