gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:53:55 25 4
gpt4 key购买 nike

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

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

字符串.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

为什么会自己变成All types?如何解决?

最佳答案

您正在 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);

关于android - 使用 Button 在 EditText 和 Spinner 中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13734732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com