0

是否可以使此代码更容易/更短?我们在 25+ 个对象上使用代码。

  Tekst = text and Knapp = buttons.
  final View v = inflater.inflate(R.layout.activity_utesteder, container, false);
  exodusTekst = (TextView) v.findViewById(R.id.exodusTekst);
  exodusKnapp = (Button) v.findViewById(R.id.exodusKnapp);

  exodusKnapp.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
          exodusTekst.setVisibility((exodusTekst.getVisibility() == View.VISIBLE) ? View.GONE : View.VISIBLE);
          exodusImage.setVisibility((exodusImage.getVisibility() == View.VISIBLE) ? View.GONE : View.VISIBLE);
          fotballpubenTekst.setVisibility(View.GONE);
          ovenpoTekst.setVisibility(View.GONE);
          kvarteretTekst.setVisibility(View.GONE);
          hulenTekst.setVisibility(View.GONE);
          sjobodenTekst.setVisibility(View.GONE);
          downstairsTekst.setVisibility(View.GONE);
          garageTekst.setVisibility(View.GONE);
          insideTekst.setVisibility(View.GONE);
          finnegansTekst.setVisibility(View.GONE);
          kosmoTekst.setVisibility(View.GONE);
          ricksTekst.setVisibility(View.GONE);
          lilleTekst.setVisibility(View.GONE);
          brukbarTekst.setVisibility(View.GONE);
          finckenTekst.setVisibility(View.GONE);
          scotsmanTekst.setVisibility(View.GONE);
          dreamsTekst.setVisibility(View.GONE);
          metroTekst.setVisibility(View.GONE);
          luuxTekst.setVisibility(View.GONE);
          tidiTekst.setVisibility(View.GONE);
          bryggenNightclubTekst.setVisibility(View.GONE);
          felizTekst.setVisibility(View.GONE);
          jacobAallTekst.setVisibility(View.GONE);
          kaosTekst.setVisibility(View.GONE);
          calibarTekst.setVisibility(View.GONE);
          fotballpubenImage.setVisibility(View.GONE);
          ovenpoImage.setVisibility(View.GONE);
          kvarteretImage.setVisibility(View.GONE);
          hulenImage.setVisibility(View.GONE);
          sjobodenImage.setVisibility(View.GONE);
          downstairsImage.setVisibility(View.GONE);
          garageImage.setVisibility(View.GONE);
          insideImage.setVisibility(View.GONE);
          finnegansImage.setVisibility(View.GONE);
          kosmoImage.setVisibility(View.GONE);
          ricksImage.setVisibility(View.GONE);
          lilleImage.setVisibility(View.GONE);
          brukbarImage.setVisibility(View.GONE);
          finckenImage.setVisibility(View.GONE);
          scotsmanImage.setVisibility(View.GONE);
          dreamsImage.setVisibility(View.GONE);
          metroImage.setVisibility(View.GONE);
          luuxImage.setVisibility(View.GONE);
          tidiImage.setVisibility(View.GONE);
          bryggenNightclubImage.setVisibility(View.GONE);
          felizImage.setVisibility(View.GONE);
          jacobAallImage.setVisibility(View.GONE);
          kaosImage.setVisibility(View.GONE);
          calibarImage.setVisibility(View.GONE);
      }
  });
  exodusImage = (ImageButton) v.findViewById(R.id.exodusImage);

  exodusImage.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
          Intent intent = new Intent();
          intent.setAction(Intent.ACTION_VIEW);
          intent.addCategory(Intent.CATEGORY_BROWSABLE);
          intent.setData(Uri.parse("http://goo.gl/maps/OEFPh"));
          startActivity(intent);
      }
  });
  fotballpubenTekst = (TextView) v.findViewById(R.id.fotballpubenTekst);
  fotballpubenKnapp = (Button) v.findViewById(R.id.fotballpubenKnapp);

  fotballpubenKnapp.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
          fotballpubenTekst.setVisibility((fotballpubenTekst.getVisibility() == View.VISIBLE) ? View.GONE : View.VISIBLE);
          fotballpubenImage.setVisibility((fotballpubenImage.getVisibility() == View.VISIBLE) ? View.GONE : View.VISIBLE);
          exodusTekst.setVisibility(View.GONE);
          ovenpoTekst.setVisibility(View.GONE);
          kvarteretTekst.setVisibility(View.GONE);
          hulenTekst.setVisibility(View.GONE);
          sjobodenTekst.setVisibility(View.GONE);
          downstairsTekst.setVisibility(View.GONE);
          garageTekst.setVisibility(View.GONE);
          insideTekst.setVisibility(View.GONE);
          finnegansTekst.setVisibility(View.GONE);
          kosmoTekst.setVisibility(View.GONE);
          ricksTekst.setVisibility(View.GONE);
          lilleTekst.setVisibility(View.GONE);
          brukbarTekst.setVisibility(View.GONE);
          finckenTekst.setVisibility(View.GONE);
          scotsmanTekst.setVisibility(View.GONE);
          dreamsTekst.setVisibility(View.GONE);
          metroTekst.setVisibility(View.GONE);
          luuxTekst.setVisibility(View.GONE);
          tidiTekst.setVisibility(View.GONE);
          bryggenNightclubTekst.setVisibility(View.GONE);
          felizTekst.setVisibility(View.GONE);
          jacobAallTekst.setVisibility(View.GONE);
          kaosTekst.setVisibility(View.GONE);
          calibarTekst.setVisibility(View.GONE);
          exodusImage.setVisibility(View.GONE);
          ovenpoImage.setVisibility(View.GONE);
          kvarteretImage.setVisibility(View.GONE);
          hulenImage.setVisibility(View.GONE);
          sjobodenImage.setVisibility(View.GONE);
          downstairsImage.setVisibility(View.GONE);
          garageImage.setVisibility(View.GONE);
          insideImage.setVisibility(View.GONE);
          finnegansImage.setVisibility(View.GONE);
          kosmoImage.setVisibility(View.GONE);
          ricksImage.setVisibility(View.GONE);
          lilleImage.setVisibility(View.GONE);
          brukbarImage.setVisibility(View.GONE);
          finckenImage.setVisibility(View.GONE);
          scotsmanImage.setVisibility(View.GONE);
          dreamsImage.setVisibility(View.GONE);
          metroImage.setVisibility(View.GONE);
          luuxImage.setVisibility(View.GONE);
          tidiImage.setVisibility(View.GONE);
          bryggenNightclubImage.setVisibility(View.GONE);
          felizImage.setVisibility(View.GONE);
          jacobAallImage.setVisibility(View.GONE);
          kaosImage.setVisibility(View.GONE);
          calibarImage.setVisibility(View.GONE);
      }
  });
  fotballpubenImage = (ImageButton) v.findViewById(R.id.fotballpubenImage);

  fotballpubenImage.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
          Intent intent = new Intent();
          intent.setAction(Intent.ACTION_VIEW);
          intent.addCategory(Intent.CATEGORY_BROWSABLE);
          intent.setData(Uri.parse("http://goo.gl/maps/h9mRw"));
          startActivity(intent);
      }
  });
4

3 回答 3

1

是的。您可以创建一种可以解决问题的方法:

private void hide(View... widgets) {
   for (View v: widgets) {
      v.setVisibility(View.GONE);
   }
}

等效于显示小部件(如果您想使用它):

private void show(View... widgets) {
   for (View v: widgets) {
      v.setVisibility(View.VISIBLE);
   }
}

由于所有小部件都从您扩展而来,View因此您可以对任何小部件使用上述方法而无需担心。

用法(针对您的情况):

hide(exodusTekst, exodusImage, fotballpubenTekst, ovenpoTekst);
于 2013-10-31T11:29:11.290 回答
0

为所有图像创建一个 setVisibility 方法,然后使用该方法。

或使用任何布局中的所有视图,并且该布局将被隐藏...

于 2013-10-31T11:30:37.463 回答
0

如果有太多视图需要更改,为什么不创建两个布局并通过调用setContentView()然后在它们之间切换,然后onResume()将您的onCreate()长实现移动到onResume()

于 2013-10-31T11:57:36.690 回答