2013-06-24 231 views
1

我必須在preferncescreen中有很多checkbox首選項,並且必須註冊每個checkbox值並根據它的開啓或關閉執行一些任務。如何在checkbox首選項中執行此操作? 我知道用下面的代碼來做到這一點的複選框:Checkboxpreference監聽器

public void onCheckboxClicked(View view) { 
      // Is the view now checked? 
     boolean checked = ((CheckBox) view).isChecked(); 

     // Check which checkbox was clicked 
     switch(view.getId()) { 
      case R.id.Activate: 
      if (checked){ 
       // Put some meat on the sandwich 
       Toast.makeText(Daydream.this, "checked!", Toast.LENGTH_LONG).show(); 
      }else 
       // Remove the meat 
       Toast.makeText(Daydream.this, "UNchecked!", Toast.LENGTH_LONG).show(); 
      break; 
     case R.id.sencond_id: 
      if (checked){ 
       // Cheese me 
      }else 
       // I'm lactose intolerant 
      break; 
     // TODO: Veggie sandwich 
    } 
} 

但我不知道我知道preferencescreen已被棄用,以任何方式做像上面這樣做有checkboxpreference.Is有,但我有什麼打算?使用它,因爲簡單,使listview.I的兩行希望我得到的答覆soon.Thanks

回答

4

使用它是這樣的

final Preference otherpref = (Preference) findPreference("otherpref"); 
final Preference pref = (Preference) findPreference("checkbox");   

pPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { 

public boolean onPreferenceClick(Preference preference) { 

    Toast.makeText(getBaseContext(), "Some text", Toast.LENGTH_SHORT).show(); 
    return true; 
} 
}); 

otherpref .setOnPreferenceClickListener(new OnPreferenceClickListener() { 

public boolean onPreferenceClick(Preference preference) { 

    Toast.makeText(getBaseContext(), "Other text", Toast.LENGTH_SHORT).show(); 
    return true; 
} 
}); 
+0

謝謝...雖然這不是我一直在尋找,它爲我工作。 – user2515577