在做项目中,遇到在联网或从数据库读取数据时,需要将当前界面变暗,并将布局内所有控件设置为不可点击状态。刚开始尝试用 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); //只需如此设置,即可达到效果
}
}