gpt4 book ai didi

java - 如何使用方法返回的 boolean 值

转载 作者:行者123 更新时间:2023-12-01 06:55:46 26 4
gpt4 key购买 nike

我只是想知道如何使用从方法返回的 boolean 值。这是返回值的方法:

 public  boolean hasConnection() {
ConnectivityManager cm = (ConnectivityManager) MCQ.this.getBaseContext().getSystemService(
Context.CONNECTIVITY_SERVICE);

NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnectedOrConnecting()) {
return true;
}

NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnectedOrConnecting()) {
return true;
}

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
return true;
}

return false;
}

这是我想使用该值的方法:

public void setScrollViewLayoutMarginBottom()
{
Resources resources = this.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();

Boolean b = hasConnection();

if(b == true)
{
px = 90 * (metrics.densityDpi/160f);
}
else
px = 60 * (metrics.densityDpi/160f);


layoutParams.bottomMargin = (int) px;
layoutParams.setMargins(0, 0, 0, (int) px);
sv.setLayoutParams(layoutParams);
}

请帮助我。提前致谢。

最佳答案

您已经在使用返回值,尽管您将其装箱为 Boolean 而不是仅仅使用 boolean ,没有特殊原因 - 并且您明确将其与 true 进行比较,这是不寻常的。

实际上,我可能会使用条件运算符:

int scale = hasConnection() ? 90 : 60;
px = scale * (metrics.densityDpi / 160f);

关于java - 如何使用方法返回的 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12550804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com