public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sp=getSharedPreferences("file1",Context.MODE_PRIVATE);
String clr=sp.getString("color","Not Found");
dropDown();
Button btn= (Button) findViewById(R.id.button);
btn.setText(clr);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
colorChange();
}
});
}
public void dropDown(){
Spinner spin=(Spinner) findViewById(R.id.spinner);
List<String> list;
SharedPreferences sp=getSharedPreferences("file1",Context.MODE_PRIVATE);
// int size=sp.getInt()
String clr=sp.getString("color","Not Found");
list=new ArrayList<>(Arrays.asList(clr));
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
R.layout.support_simple_spinner_dropdown_item,list);
adp.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spin.setAdapter(adp);
}
public void colorChange(){
EditText ed1=(EditText) findViewById(R.id.editText);
String clr= ed1.getText().toString().toLowerCase();
SharedPreferences sp1=getSharedPreferences("file1", Context.MODE_PRIVATE);
SharedPreferences.Editor ed=sp1.edit();
ed.putString("color",clr);
ed.apply();
ed.commit();
}
}
如何在微调器中的共享首选项中显示我的所有值? 我正在创建一个应用程序,它从用户获取颜色名称并根据给定的颜色更改背景,并将该值存储在微调器(组合框)中,并将该选定值保持在该组合框的顶部。