android 将整个布局设置为不可点击状态

在做项目中,遇到在联网或从数据库读取数据时,需要将当前界面变暗,并将布局内所有控件设置为不可点击状态。刚开始尝试用 page1.setClickable(false);的方法,以及焦点控制的方法都未能达到效果。后来想到其实解决方法非常简单,因为page2是完全覆盖在page1之上,所以只需将page2的点击效果取消即可。可参考一下代码




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/page1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="50dp"
            android:onClick="test1"
            android:text="@string/hello_world"
            android:textSize="15sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:onClick="test2"
            android:text="@string/hello_world"
            android:textSize="15sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="按钮1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="按钮2" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/page2"
        android:layout_width="match_parent"
        android:visibility="gone"
        android:layout_height="match_parent" >
        
        <ProgressBar 
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerInParent="true"
            android:background="#000000"
            />
    </RelativeLayout>

</RelativeLayout>

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		LinearLayout page1=(LinearLayout) findViewById(R.id.page1);
		RelativeLayout page2=(RelativeLayout) findViewById(R.id.page2);
		page1.setAlpha((float) 0.3);
		page2.setVisibility(View.VISIBLE);
//		page1.setClickable(false);
		
		page2.setOnClickListener(null);  //只需如此设置,即可达到效果
		
		
	}


}




  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值