0

我有一个微调器,它在自动选择时启动活动。我如何有一个提交按钮,以便用户可以首先选择他们想要启动的活动,然后点击提交以转到选定的屏幕?这是我目前拥有的:

 private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d("TEST", "this is a test");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.entries,
            android.R.layout.simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new function());

}


public class function implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
            long id) {
        String selection = ((TextView)arg1).getText().toString();
        if (selection.equals("Red Screen")){
            Intent intent = new Intent(arg1.getContext(), DisplayMessageActivity.class);
            startActivity(intent);
        }else if(selection.equals("Math Screen")){
            Intent intent = new Intent(arg1.getContext(), Numbers.class);
            startActivity(intent);
        }else{
            Intent intent = new Intent(arg1.getContext(), Face.class);
            startActivity(intent);
        }
4

3 回答 3

1

尝试这个 :

 private Class activityToOpen;

 private Spinner spinner;
 private Button button;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     spinner = (Spinner) findViewById(R.id.spinner1);
     button= (Button) findViewById(R.id.button);

     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.entries,
        android.R.layout.simple_spinner_dropdown_item);
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     spinner.setAdapter(adapter);
     spinner.setOnItemSelectedListener(new function());
     button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
                    if(activityToOpen!=null)
                    {
                    Intent intent = new Intent(view.getContext(), activityToOpen);
                    startActivity(intent);

        }
    });
}

public class function implements OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
        long id) {
    String selection = ((TextView)arg1).getText().toString();
    if (selection.equals("Red Screen")){
        activityToOpen = DisplayMessageActivity.class;
    }else if(selection.equals("Math Screen")){
        activityToOpen = Numbers.class;
    }else{
        activityToOpen = Face.class;
    }
于 2013-06-21T20:45:05.727 回答
0

oncreate Disable按钮很简单

如果OnItemSelectedListener选择了该选项,则启用该按钮。因此,您强制用户从列表中选择任何选项(微调器)

于 2013-06-21T20:18:25.740 回答
0

在您的OnClickListener中,您应该阅读 的选择Spinner并开始适当的活动。您应该查看Android 培训教程Android API 指南以获取有关如何执行此操作的信息。在查阅完这些文件后,当您遇到困难时,请随时提出具体问题。

于 2013-06-21T20:47:42.050 回答