我想在检查互联网雕像后更改图像视图图像。 基本的,我想检查互联网是否有效,如果我想要显示某个图像。
这里是我的代码:
public void onClick(View v) {
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection", "You have internet connection", true);
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection", "You don't have internet connection.", false);
}
}
答案 0 :(得分:0)
我假设你知道在Android上检查互联网。
在oncreate中使用:
imagev = (ImageView)findviewbyid(R.id.imageview);
onbutton click:
public void onClick(View v) {
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
"You have internet connection", true);
imagev.setImageDrawable(getResources().getDrawable(R.drawable.withinternet));
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
"You don't have internet connection.", false);
imagev.setImageDrawable(getResources().getDrawable(R.drawable.nointernet));
}
答案 1 :(得分:0)
使用以下方法检查互联网是否可用
public static boolean isOnline(Activity context) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
在清单文件中设置以下权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
然后检查如下
if(isOnline())
img.setImageResource();
else
img.setImageResource();
答案 2 :(得分:0)
尝试更改ImageView
的图像。
your_ImageView.setBackgroundResource(R.drawable.your_image_name);