有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当location==null时,java会更改按钮的可见性

在方法onCreate中,我有一个按钮:

    connection.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            {
                   GoogleApiClient.connect();
            }

在最底层,安卓 studio生成:

@Override

public void onConnected(Bundle bundle) 
    {

    if (ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 安卓.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时隐藏我的连接按钮,并取消隐藏“else”

我在stack上发现了这个方法:

button.setEnabled(false/true);

但我现在不知道该把这个方法放在哪里

该怎么做才能让它发挥作用


共 (1) 个答案

  1. # 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);
    }