gpt4 book ai didi

java - 我必须按什么顺序编码?

转载 作者:行者123 更新时间:2023-12-01 10:51:44 25 4
gpt4 key购买 nike

在我的应用程序中,用户可以在微调器中选择某些内容,并在单击按钮时收到包含所选文本的 Toast。

现在就我而言,当他单击按钮时,会弹出一个 toast ,但是是空的。我认为 toast 的共享首选项不会实现,因为它们不会在代码行的开头被识别?看看:

.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = this.getSharedPreferences("spinnerSelection", 0);
btn = (Button) findViewById(R.id.send);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

spinnergipfeli.setSelection(prefs.getInt("spinnerSelection", 0));
Context context = getApplicationContext();
CharSequence text = (thetime);
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
.
.

以及后面的代码:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
editor = prefs.edit();
selectedPosition = spinnergipfeli.getSelectedItemPosition();
String Text = spinnergipfeli.getSelectedItem().toString();
editor.putInt("spinnerSelection", selectedPosition);
editor.putString("thetime", Text);
editor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}

最佳答案

你的代码工作得很好。您只需检索“thetime”条目即可获取您要查找的字符串:

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

spinnergipfeli.setSelection(prefs.getInt("spinnerSelection", 0));
Context context = getApplicationContext();
String text = prefs.getString("thetime", "default empty value");
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
});

关于java - 我必须按什么顺序编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33838540/

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