如何缩短这个? (方法)

时间:2013-10-31 11:19:42

标签: android xml onclick onclicklistener

是否可以使此代码更容易/更短? 我们在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);
      }
  });

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

或使用任何布局中的所有视图,只是该布局将隐藏...

答案 2 :(得分:0)

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