Android将微调器值发送到其他活动

时间:2016-01-04 14:52:09

标签: android android-activity android-studio spinner

我正在构建一个应用程序,在我的主要活动中,我有一个微调器,现在我希望在按下按钮时将所选值发送到我的其他活动。

我已经成功完成了几个EditText。但对于一个微调器来说似乎有点棘手。

我正在关注de developer.android.com网站上的文档,但我真的不明白。链接:http://developer.android.com/guide/topics/ui/controls/spinner.html

我在代码中用旋转数组中的数据填充微调器: 我在onCreate方法中执行此代码:

//supply spinner with the array using an instance of ArrayAdapter
    Spinner spinner = (Spinner) findViewById(R.id.timespan_spinner);
    //create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.timespan_spinner, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

这是我将EditText的数据发送到我的其他活动的方式: 这是我的sendForm方法

public void sendForm(View view) {
    //creating an Intent
    Intent intent = new Intent(this, DisplayFormActivity.class);
    //defining fields
    EditText editTextFirstname = (EditText) findViewById(R.id.txtFirstname);
    EditText editTextLastname = (EditText) findViewById(R.id.txtLastname);
    EditText editTextBedrag = (EditText) findViewById(R.id.txtBedrag);

    //getting the field values
    String firstname = editTextFirstname.getText().toString();
    String lastname = editTextLastname.getText().toString();
    String bedrag = editTextBedrag.getText().toString();

    //putting data in the intent
    intent.putExtra(FIRSTNAME, firstname);
    intent.putExtra(LASTNAME, lastname);
    intent.putExtra(BEDRAG, bedrag);

    startActivity(intent);
}

1 个答案:

答案 0 :(得分:2)

你可以看看this回答,引用:

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();

所以,对于你的代码:

public void sendForm(View view) {
        //creating an Intent
        Intent intent = new Intent(this, DisplayFormActivity.class);
        //defining fields
        EditText editTextFirstname = (EditText) findViewById(R.id.txtFirstname);
        EditText editTextLastname = (EditText) findViewById(R.id.txtLastname);
        EditText editTextBedrag = (EditText) findViewById(R.id.txtBedrag);
        Spinner spinner = (Spinner) findViewById(R.id.timespan_spinner);


        //getting the field values
        String firstname = editTextFirstname.getText().toString();
        String lastname = editTextLastname.getText().toString();
        String bedrag = editTextBedrag.getText().toString();
        String chosenOption = spinner.getSelectedItem().toString();

        //putting data in the intent
        intent.putExtra(FIRSTNAME, firstname);
        intent.putExtra(LASTNAME, lastname);
        intent.putExtra(BEDRAG, bedrag);
        intent.putExtra(CHOSEN_OPTION, chosenOption);

        startActivity(intent);
    }