Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实施;“关于失去焦点”;Android中任何视图的效果_Android_Onclick - Fatal编程技术网

实施;“关于失去焦点”;Android中任何视图的效果

实施;“关于失去焦点”;Android中任何视图的效果,android,onclick,Android,Onclick,我在活动上有一个按钮和许多其他视图组件。我想在单击屏幕上的任何其他位置时隐藏按钮(就像按钮失去焦点一样)。所以我用 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="ma

我在活动上有一个按钮和许多其他视图组件。我想在单击屏幕上的任何其他位置时隐藏按钮(就像按钮失去焦点一样)。所以我用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_page"
    android:onClick="randomClick" >
  <Button
    android:id="@+id/button_to_hide" />
  <ImageView
    android:onClick="zoomView"
    android:src="..." />
</RelativeLayout>
问题是

(1) 当我碰巧点击带有
onClick
监听器的
ImageView
时,
onClick
监听器将被触发,转到另一个活动。我还不想转到“缩放视图”活动,因为我想先隐藏按钮。
ImageView
只是一个例子,还有许多其他可单击的视图,所以我不想逐个更改像
zoomView()
这样的函数


(2) 一个小问题是,尽管我将“
match\u parent
”设置为主布局,但当我单击屏幕底部时,按钮没有隐藏?(内容没有占据整个屏幕的高度)

我为此制作了一个样本。检查一下。它能帮到你吗

public class MainActivity extends Activity implements OnClickListener {

RelativeLayout layout;
Button btnClick;
ImageView imageClick;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (RelativeLayout) findViewById(R.id.layout);

    btnClick = (Button) findViewById(R.id.button);
    imageClick = (ImageView) findViewById(R.id.imageView);

    layout.setOnClickListener(this);
    btnClick.setOnClickListener(this);
    imageClick.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()) {
    case R.id.layout:

        if (btnClick.getVisibility() == View.VISIBLE) {
            btnClick.setVisibility(View.INVISIBLE);
        } else if (btnClick.getVisibility() == View.INVISIBLE) {
            btnClick.setVisibility(View.VISIBLE);
        }

        break;
    case R.id.button:
        Toast.makeText(MainActivity.this, "Click Button",
                Toast.LENGTH_SHORT).show();
        break;
    case R.id.imageView:
        Toast.makeText(MainActivity.this, "Click Imageview",
                Toast.LENGTH_SHORT).show();
        break;

    default:
        break;
    }
}
}

和xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="@string/hello_world" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:clickable="true"
    android:layout_marginTop="10dip"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView"
    android:layout_marginTop="10dip"
    android:text="Click" />

编辑: 使用以下命令更改xml


在布局中添加安卓:clickable=“true”。

对于第一个问题

首先检查按钮是否隐藏,然后再开始活动

public void zoomView(View v){
   if( btn_to_hide.getVisibility()==View.VISIBLE)
         return;
   Intent intent = new Intent(...); //go to another Activity to zoom view the image
}

您必须确保在单击之前调用了
zoomView
方法。或者最好在
random单击中的按钮添加淡出效果,而不是立即将其隐藏。

将单击侦听器绑定到主布局这..可能会帮助您解决问题非常感谢!绑定到主布局是一种很有前途的方法。我在编辑时仍然有一些问题。我现在将研究这些(特别是一些视图是动态创建的,我可能没有它们的ID)。在父母和孩子的事件侦听器上调用序列的内在机制是什么?zoomView只是一个函数,可能有很多,我不想检查每个函数中的btn_to_隐藏可见性。@mrmoment取决于布局。最好是在覆盖主要内容的版面中添加按钮。覆盖版面的宽度和高度应与主要内容相同。这样,您的覆盖布局句柄首先单击,您可以将其隐藏,而不是隐藏按钮
public void zoomView(View v){
   if( btn_to_hide.getVisibility()==View.VISIBLE)
         return;
   Intent intent = new Intent(...); //go to another Activity to zoom view the image
}