gpt4 book ai didi

java - 保存和获取具有共享首选项的微调项目

转载 作者:行者123 更新时间:2023-12-01 19:02:23 26 4
gpt4 key购买 nike

我在将微调项目保存到共享首选项时遇到问题。我所说的问题是我真的不知道该怎么做。如果有人知道的话,如果有人能真正向我解释,那就太好了。提前致谢:)

所以我设置了按钮 1 以打开带有微调器的对话框,并且在微调器中我有 3 个项目(在 strings.xml 中声明)。我应该使用共享首选项将微调器默认设置为项目 1:“-”,如果用户选择项目 2“lang1”或“lang2”,则应将其保存在应用程序中。这样,当我下次打开应用程序时,它的“lang1”或“lang2”取决于选择。

其他一切都工作正常。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";

int spinnerPosition;
public Button btnTrans, btnSelect,btnInput,btnCheck;
public TextView tv_title, tv_message;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnSelect= (Button) findViewById(R.id.btnLanguage);
btnInput = (Button) findViewById(R.id.btnInput);
btnTrans = (Button) findViewById(R.id.btnTranslate);

btnTrans.setOnClickListener((View.OnClickListener) this);
btnInput.setOnClickListener(this);
btnSelect.setOnClickListener(this);

Spinner lang_spinner = (Spinner) findViewById(R.id.lang_spinner);

}

public void onClick(View v)
{
int id=v.getId();

switch (id){
case R.id.btnLanguage:

AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.language_dialog, null);
mBuilder.setTitle("select language");
final Spinner mSpinner = (Spinner) mView.findViewById(R.id.lang_spinner);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.lang_array));

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
SharedPreferences.Editor editor = sharedpreferences.edit();

//SELECTING LANGUAGE switch case (translate btn)-
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
Object item = adapterView.getItemAtPosition(position);
if (item != null) {

switch (position) {
case 0:
item = "-";
btnTrans.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.nista, Toast.LENGTH_LONG).show();
}
});
break;
case 1:
item = "lang1";
btnTrans.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.hrvatski, Toast.LENGTH_LONG).show();
}
});
break;
case 2:
item = "lang2";
btnTrans.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.english, Toast.LENGTH_LONG).show();
}
});
break;
}

}
//on selected save

}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {
//nothing selected "-"
}
});



最佳答案

我的代表人数不到 50,所以我在这里发布链接以共享偏好 https://www.google.com/amp/s/www.geeksforgeeks.org/shared-preferences-in-android-with-examples/amp/

从微调器中获取您的 Lang 并将值存储在共享偏好中。

关于java - 保存和获取具有共享首选项的微调项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59614967/

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