我有 if else 条件的问题,当我选择一些选项(radioButtons)然后将它们推送到活动以给出结果时,我有片段。我试图通过多态性获得一些方法,但我仍然无法获得与 if else 语句相同的结果。在这种情况下,SharedPreferences 可以用某些东西代替,以从另一个片段中获取结果吗?感谢帮助。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_result);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String size = sharedPref.getString("size", "");
String activity = sharedPref.getString("activity", "");
String children = sharedPref.getString("children", "");
String dog = sharedPref.getString("dog", "");
TextView dogName = findViewById(R.id.dogName);
TextView dogName2 = findViewById(R.id.dogName2);
dogName.setText(dog);
dogName2.setText(dog);
ImageSwitcher switchImageView = findViewById(R.id.imageSwitcher);
ImageView imageView2 = findViewById(R.id.imageView2);
imageView2.setImageDrawable(null);
switchImageView.setFactory(() -> {
ImageView imageView = new ImageView(getApplicationContext());
if (size.equalsIgnoreCase("small") &&
activity.equalsIgnoreCase("lazy") &&
children.equalsIgnoreCase("like")) {
imageView.setImageResource(R.drawable.bulldog);
dogName.setText("Bulldog");
}else if (
size.equalsIgnoreCase("miniature") &&
activity.equalsIgnoreCase("light active") &&
children.equalsIgnoreCase("like")
) {
imageView.setImageResource(R.drawable.chihuahua);
dogName.setText("Chihuahua");
imageView2.setImageResource(R.drawable.york);
dogName2.setText("York");
} else if (
size.equalsIgnoreCase("miniature") &&
activity.equalsIgnoreCase("light active") &&
children.equalsIgnoreCase("no matter")
) {
imageView.setImageResource(R.drawable.york);
dogName.setText("York");
}
if 语句更长,但我只是想展示该方法。