启动主活动时设置可见性时出错



我只是想制作一个应用程序来检查我的互联网连接是否打开。如果互联网连接不可用,则它将显示一个单独的LinearLayout和完整的match_parent,但在可见性下无法工作。任何帮助都将不胜感激。

<RelativeLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:background="#c6c6c9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/error"
android:textAlignment="center"
android:onClick="onClick"
android:gravity="center"
android:visibility="gone"
android:text="connect to Internet"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
Toast.makeText(this, "Take a Hotspot Dude...", Toast.LENGTH_SHORT).show();
r1.setVisibility(View.VISIBLE);
return false;
}
return true;
}

如果您想在确定连接状态后在布局之间切换,请考虑完全使用两个不同的布局,并将ContentView设置为适合当前条件的布局。

public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
Toast.makeText(this, "Take a Hotspot Dude...", Toast.LENGTH_SHORT).show();
r1.setVisibility(View.VISIBLE);
setContentView(R.layout.network_doesnt_exist);
return false;
} else {
setContentView(R.layout.network_exists)
}

或者,如果你想在布局中的项目之间切换,这可能会有点混乱,请将可见性状态设置为"GONE">

textView.setVisibility(View.GONE);

将此选项用于要使其从布局中完全消失的任何项目,这意味着它们不仅被隐藏,而且不包括在任何layout_weight或此类分布中。

我希望这有助于回答你的问题!

最新更新