当location == null

时间:2016-10-27 20:02:50

标签: java android google-maps

在方法onCreate我有按钮:

    connection.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            {
                   GoogleApiClient.connect();
            }

并在最底层的android工作室生成:

@Override

public void onConnected(Bundle bundle) 
    {

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;

    }

    Location location = LocationServices.FusedLocationApi.getLastLocation(GoogleApiClient);

    if (location == null) 
    {

                LocationServices.FusedLocationApi.requestLocationUpdates(GoogleApiClient, LocationRequest, (LocationListener) this);

    }

现在我描述我的问题: 当location == null时,尝试隐藏我的连接按钮,并且'否则'取消隐藏。

我在堆栈上发现了这种方法:

button.setEnabled(false/true);

但我现在不应该放置这种方法

如何使其发挥作用?

2 个答案:

答案 0 :(得分:1)

使用隐形或可见按钮:

if (location == null) {
    button.setVisibility(View.INVISIBLE); // remove the element but used the space.
            or
    button.setVisibility(View.GONE); // remove element with used space taken by that element
}else{
    button.setVisibility(View.VISIBLE);
}

答案 1 :(得分:0)

使用View.setVisibility(int)隐藏 - 选项为View.GONE(完全隐藏视图,其他视图填充其空间); View.INVISIBLE(隐藏视图,但它仍占用布局空间); View.VISIBLE(显示视图,再次占据空间)。

所以人们之前评论过:

if (location == null) {
    myButton.setVisibility(View.GONE);
} else {
    myButton.setVisibility(View.VISIBLE);
}