1

我想在检查 Internet 状态后更改 imageview 图像。基本的,如果我想显示某个图像,我想检查互联网是否正常工作。

这是我的代码:

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);
    }
}
4

3 回答 3

0

我假设您了解在 Android 中检查互联网。

在 oncreate 使用这个:

imagev = (ImageView)findviewbyid(R.id.imageview); 

按钮点击:

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));
                }
于 2012-10-31T07:09:58.060 回答
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();
于 2012-10-31T07:10:04.317 回答
0

试试这个来改变ImageView.

 your_ImageView.setBackgroundResource(R.drawable.your_image_name);
于 2012-10-31T07:18:04.360 回答