我有一个带有一些按钮的 OnClickListener。因此,例如,当我按下其中一些按钮时,屏幕底部会出现另一个按钮。如果我不点击最后一个按钮,这个按钮必须消失。
问题是:我怎么知道用户是点击了按钮还是屏幕的另一部分。
如果用户单击了我可以使用 onClick() 的按钮吗?如果用户没有点击按钮,什么功能得到那个动作?
PD:我尝试使用 onUserInteraction() 并无法解决此问题,因为程序调用它是否单击了按钮。
我的 XML 代码是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/paco"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/pepe"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
</RelativeLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp" >
</TabWidget>
</FrameLayout>
</TabHost>
<LinearLayout
android:id="@+id/pedro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/layoutCuadricula_anadirRegalo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/btCuadricula_foto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foto" />
<Button
android:id="@+id/btCuadricula_galeria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btCuadricula_foto"
android:text="Galeria" />
<Button
android:id="@+id/btCuadricula_iconos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btCuadricula_galeria"
android:text="Iconos" />
</RelativeLayout>
<Button
android:id="@+id/btCuadricula_anadir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Añadir" />
</LinearLayout>
</FrameLayout>
好吧,当我按下按钮“Añadir”时,会出现带有另外 3 个按钮的 RelativeLayout。如果我的下一个动作不是按下任何那个按钮,RelativeLayout。
我显示我的代码:
public class Cuadricula extends TabActivity implements OnClickListener {
private Button btAnadir, btFoto, btGaleria, btIcono;
private AlphaAnimation alpha;
private TranslateAnimation translate;
private RelativeLayout cuadroBotones;
private AnimationSet animationSet;
private LinearLayout pedro;
private FrameLayout pepe, paco;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cuadricula);
pedro = (LinearLayout) this.findViewById(R.id.pedro);
pepe = (FrameLayout) this.findViewById(R.id.pepe);
paco = (FrameLayout) this.findViewById(R.id.paco);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabSpec primeraPestana = tabHost.newTabSpec("tid1");
TabSpec segundaPestana = tabHost.newTabSpec("tid2");
TabSpec terceraPestana = tabHost.newTabSpec("tid3");
TabSpec cuartaPestana = tabHost.newTabSpec("tid4");
primeraPestana.setIndicator("Timeline").setContent(
new Intent(this, Timeline.class));
segundaPestana.setIndicator("Eventos").setContent(
new Intent(this, Personas.class));
terceraPestana.setIndicator("Perfil").setContent(
new Intent(this, Perfil.class));
cuartaPestana.setIndicator("Top").setContent(
new Intent(this, Top.class));
tabHost.addTab(primeraPestana);
tabHost.addTab(segundaPestana);
tabHost.addTab(terceraPestana);
tabHost.addTab(cuartaPestana);
cuadroBotones = (RelativeLayout) this
.findViewById(R.id.layoutCuadricula_anadir);
cuadroBotones.setVisibility(RelativeLayout.GONE);
btFoto = (Button) this.findViewById(R.id.btCuadricula_foto);
btGaleria = (Button) this.findViewById(R.id.btCuadricula_galeria);
btIcono = (Button) this.findViewById(R.id.btCuadricula_iconos);
btAnadir = (Button) this
.findViewById(R.id.btCuadricula_anadirRegalo);
btFoto.setOnClickListener(this);
btGaleria.setOnClickListener(this);
btIcono.setOnClickListener(this);
btAnadir.setOnClickListener(this);
paco.setOnClickListener(this);
pedro.setOnClickListener(this);
pepe.setOnClickListener(this);
// Transiciones del cuadro de botones
alpha = new AlphaAnimation(0.0f, 1.0f);
alpha.setDuration(1000);
alpha.setFillAfter(true); // Tell it to persist after the animation ends
translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0);
translate.setDuration(500);
animationSet = new AnimationSet(true);
animationSet.addAnimation(alpha);
animationSet.addAnimation(translate);
}
public void onResume() {
super.onResume();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btCuadricula_anadirRegalo:
cuadroBotones.setVisibility(RelativeLayout.INVISIBLE);
cuadroBotones.startAnimation(animationSet);
botonesDesplegados = true;
break;
case R.id.btCuadricula_foto:
break;
case R.id.btCuadricula_galeria:
break;
case R.id.btCuadricula_iconos:
break;
case R.id.LinearLayout1:
case R.id.paco:
case R.id.pepe:
case R.id.pedro:
cuadroBotones.setVisibility(RelativeLayout.GONE);
break;
}
}
}