使用Button在EditText和Spinner中获取文本

时间:2012-12-05 23:54:54

标签: android android-layout android-intent android-emulator

我想同时从EditText和Spinner获取文本,但是当我选择All types以外的类型时,它会更改为All types本身。

我的问题是,在我点击发送按钮后,功能Flim会自动更改为所有类型,而不是如何在微调器中获取所选项目。

的strings.xml:

<string-array name="country_arrays">
<item>All Types</item>
<item>Feature Flim</item>
<item>TV Series</item>
<item>Video Games</item>
</string-array>

activity_main.xml中:

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:entries="@array/country_arrays"
     />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_send" 
    android:onClick="sendMessage"/>

这是MainActicity.java中的函数sendMessage:

public void sendMessage(View view) {

    final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.country_arrays, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Toast.makeText(getBaseContext(), 
                "You have selected the book: " + String.valueOf(spinner.getSelectedItem()), 
                Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString() + String.valueOf(spinner.getSelectedItem());
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

和DisplayMessageActivity.java:

public class DisplayMessageActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);


}

我选择功能片段:

I choose feature flim

点击发送后

after I click send

最后它显示:

enter image description here

为什么它会自行更改为所有类型?如何解决?

3 个答案:

答案 0 :(得分:2)

您正在sendMessage内设置适配器和数据。因此,每次单击“发送”时,Spinner都会重新填充,从而再次位于0位。

相反,请执行以下操作:

首先,在您的类声明中放置以下行:

private Spinner spinner = null;

其次,将这些内容移至onCreate()(或您拨打setContentView()的任何地方):

spinner = (Spinner) findViewById(R.id.spinner1); // Do not re-declare
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.country_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Toast.makeText(getBaseContext(), 
            "You have selected the book: " + String.valueOf(spinner.getSelectedItem()), 
            Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }
});

第三,最后,在sendMessage()方法中留下以下内容:

Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString() + String.valueOf(spinner.getSelectedItem());
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);

答案 1 :(得分:0)

尝试以其他方式获取文字:

spinner.geItemAtPosition(arg2).toString();

答案 2 :(得分:0)

要获取Spinner的选定项目,请执行以下操作:

Spinner mySpinner=(Spinner)findViewById(R.id.Spinner_Id);
mySpinner.getSelectedItem().toString();