long if-else语句

vqlkdk9b  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(412)

我对if-else条件有一个问题,当我选择一些选项(单选按钮)时会出现片段,然后它们会被推到“活动”以给出结果。我试图得到一些方法,比如by多态性,但是仍然不能得到与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语句更长,但我只是想展示一下方法。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题