使用“提交”按钮使用微调器启动新活动

时间:2013-06-21 20:13:52

标签: android button spinner

我有一个微调器,可以在自动选择活动时启动它。如何使用“提交”按钮以便用户可以先选择要启动的活动,然后点击“提交”以转到所选屏幕?这是我现在拥有的:

 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);
        }

3 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

简单的oncreate Disable按钮

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

答案 2 :(得分:0)

OnClickListener中,您应该阅读Spinner的选择并开始相应的活动。您应该查看Android Training TutorialsAndroid API Guides,了解有关如何执行此操作的信息。在完成这些文件的整理之后,当你遇到困难时,请随时回答具体问题。