具有旋转器和旋转器的活动切换按钮
我正在构建一个应用程序和这是我的应用程序的设置页面(活动视图)是 this
< /em>。
我使用一个切换按钮
& 旋转器
。
当选中切换按钮时,该应用程序的所有其他用户都可以看到用户个人资料。未选中时隐藏。
从微调器选项中,用户可以通过选择距离来搜索他/她周围该应用程序的其他用户。
我的问题是什么->当应用程序运行时,这些设置有效,但是当我重新启动我的应用程序时,用户之前的设置会丢失(即,这些设置不是持久的,并且我必须再次执行这些设置)...我如何克服这个问题。我的代码是->
public class Settings extends Activity implements OnCheckedChangeListener, OnItemSelectedListener {
ToggleButton tgb;
Spinner redai;
final String[] dis_option= new String[]{"1 mile","5 miles","10 miles","All"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
//Toggle Button
tgb = (ToggleButton) findViewById(R.id.settings_visibility);
tgb.setOnCheckedChangeListener(this);
//Spinner
redai=(Spinner)findViewById(R.id.settings_redai);
ArrayAdapter<String> sel_dis=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, dis_option);
sel_dis.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
redai.setAdapter(sel_dis);
redai.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
String temp_nearBy=parent.getItemAtPosition(position).toString();
/** preference that write the selected distance for further references in other Activities */
SharedPreferences nearByDistanceWrite = getSharedPreferences("nearBy", MODE_WORLD_READABLE);
SharedPreferences.Editor edit = nearByDistanceWrite.edit();
edit.putString("upToDistance", temp_nearBy);
edit.commit();
Log.i("Settings_Spinner", "onItemSelected_pref_edited..."+temp_nearBy);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (!isChecked) {
buttonView.getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Hide Your Profile?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//here, I pass Visibility as '0' which means profile of fan is Hidden & not seen to other fans!
setVisibility(0);//Method which set Connection with sever & set the visibility of user profile accordingly
/** Here you have to write code to set device token as '0' to hide the Profile of Fan! */
final ProgressDialog pg= ProgressDialog.show(Settings.this, "Progress...", "please wait updating settings!",true);
Toast.makeText(Settings.this,"Your Profile Set Hidden Successfully!", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//setting toggle button to unchecked
ToggleButton tg=(ToggleButton)findViewById(R.id.settings_visibility);//buttonView.setChecked(false);
tg.setChecked(true);
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
//here, I pass Visibility as '1' which means profile of fan is visible to other fans
setVisibility(1);
Toast.makeText(Settings.this,"Your Profile get Visible Successfully!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("Settings_onResume", "onresume_after(new)..."+nearBy);
}
/** Method that set connection with server & Set the visibility of Fan Profile to either Visible(1) or Hidden(0)
* @param visibility : The int value which indicate to set profile of a Fan to be Visible or Hidden. */
private void setVisibility(int visibility) {
//...more code ...
}
}
当我重新启动我的应用程序时,如何获取这些设置
(旧设置或我以前的设置)...??? &
如何使 Spinner
视图更加引人注目好看(系统微调器看起来不太好)???
我很高兴有指针
或代码片段
来解决这个问题问题!
I am building one app & this is the setting page(Activity view) of my app is this
.
I use one Toggle Button
& Spinner
.
When Toggle Button is checked the user profile is visible to all other users of this app & is Hidden when it is unchecked.
From Spinner options, user can search others users of this app around him/her by selecting distance.
What my problem is -> When app is running these settings working but when i relaunch my app user`s previous setting get lost(i.e., that are not persistent & again i have to do these settings)... How I can over come from this problem. My code is->
public class Settings extends Activity implements OnCheckedChangeListener, OnItemSelectedListener {
ToggleButton tgb;
Spinner redai;
final String[] dis_option= new String[]{"1 mile","5 miles","10 miles","All"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
//Toggle Button
tgb = (ToggleButton) findViewById(R.id.settings_visibility);
tgb.setOnCheckedChangeListener(this);
//Spinner
redai=(Spinner)findViewById(R.id.settings_redai);
ArrayAdapter<String> sel_dis=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, dis_option);
sel_dis.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
redai.setAdapter(sel_dis);
redai.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
String temp_nearBy=parent.getItemAtPosition(position).toString();
/** preference that write the selected distance for further references in other Activities */
SharedPreferences nearByDistanceWrite = getSharedPreferences("nearBy", MODE_WORLD_READABLE);
SharedPreferences.Editor edit = nearByDistanceWrite.edit();
edit.putString("upToDistance", temp_nearBy);
edit.commit();
Log.i("Settings_Spinner", "onItemSelected_pref_edited..."+temp_nearBy);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (!isChecked) {
buttonView.getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Hide Your Profile?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//here, I pass Visibility as '0' which means profile of fan is Hidden & not seen to other fans!
setVisibility(0);//Method which set Connection with sever & set the visibility of user profile accordingly
/** Here you have to write code to set device token as '0' to hide the Profile of Fan! */
final ProgressDialog pg= ProgressDialog.show(Settings.this, "Progress...", "please wait updating settings!",true);
Toast.makeText(Settings.this,"Your Profile Set Hidden Successfully!", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//setting toggle button to unchecked
ToggleButton tg=(ToggleButton)findViewById(R.id.settings_visibility);//buttonView.setChecked(false);
tg.setChecked(true);
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
//here, I pass Visibility as '1' which means profile of fan is visible to other fans
setVisibility(1);
Toast.makeText(Settings.this,"Your Profile get Visible Successfully!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("Settings_onResume", "onresume_after(new)..."+nearBy);
}
/** Method that set connection with server & Set the visibility of Fan Profile to either Visible(1) or Hidden(0)
* @param visibility : The int value which indicate to set profile of a Fan to be Visible or Hidden. */
private void setVisibility(int visibility) {
//...more code ...
}
}
How can i get these Settings
(old settings or my previous settings) when i relaunch my app...??? &
How can i make the Spinner
view more eye catching & good looking(the system spinner is not looking good)???
I pleased to have pointers
or code snippets
to solve this problem!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是针对应用程序,您应该使用 SharedPreferences 返回这些值。这正是它们的用途。访问与该特定应用程序相关的数据。
If this is for an app, you should be using your SharedPreferences to return these values. This is exactly what they are supposed to be used for. Accessing of data relevant to this specific app.