具有Spinner&切换按钮

时间:2012-01-21 13:06:24

标签: android spinner android-preferences togglebutton

我正在构建一个app&这是我的应用的设置页面(活动视图)是this 我使用了一个 Toggle Button & Spinner

选中切换按钮时,此应用的所有其他用户都可以看到用户个人资料。在未选中时隐藏。

通过Spinner选项,用户可以通过选择距离来搜索他/她周围的其他用户。

我的问题是什么 - >当应用程序正在运行这些设置工作,但当我重新启动我的应用程序用户的先前设置丢失(即,这不是持久性和再次,我必须做这些设置)...我怎么能来自这个问题。我的代码是 - >

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 ...

 }

}  

当我重新启动我的应用程序时,如何获得这些Settings(旧设置或我以前的设置)...... ??? &amp;

如何让Spinner视图更引人注目&amp;好看(系统微调器看起来不太好)???

很高兴pointerscode snippets来解决这个问题!

1 个答案:

答案 0 :(得分:0)

如果这是针对某个应用,则应使用您的SharedPreferences返回这些值。这正是它们应该用于的目的。访问与此特定应用相关的数据。