setVisibility(View.GONE)仅在调用2次

时间:2015-10-21 23:20:56

标签: android android-layout

我已经搜索了很多类似的问题,但没有运气。如果它不是一个,请不要将其标记为重复。如果需要,请随时帮我改变标题。

我有一个应用程序,它在常规活动之上显示GPS检索活动(格式化为对话框)。我的代码应该格式化对话框,以根据操作流程响应性地更改其按钮。当它开始时,这2个按钮的可见性以编程方式设置为已经消失(已经尝试设置在活动布局中,根本没有差异),然后调用方法来查找GPS定位。如果没有检索到位置,则对话框应该(并且确实)将按钮设置为可见。该对话框要求用户再次尝试或在没有建立位置的情况下继续。如果我选择继续,则对话框将省略其中一个按钮并更改可见文本的文本。如果我选择再次尝试检索位置,则对话框应该再次省略两个按钮,但这不会发生。最疯狂的是当我按下"再试一次"按钮(仍然显示)第二次,在此毛刺之后,按钮被省略。为什么会这样?

这是我的代码:

private Location mLastLocation;
private Location location;

// boolean flag to toggle periodic location updates
private boolean mRequestingLocationUpdates = false;


// UI elements
private TextView mainMsg;
Chronometer c;
private int count = 0;
private boolean proceed = false;
private Button button1, button2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_location_retriever);
    button1 = (Button) findViewById(R.id.btn_loc_1);
    button1.setVisibility(View.GONE);
    button2 = (Button) findViewById(R.id.btn_loc_2);
    button2.setVisibility(View.GONE);
    c = (Chronometer) findViewById(R.id.crono);
    c.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer chronometer) {
            if (c.getText().equals("00:05")) {
                c.stop();
                stopLocationUpdates();
                proceed = false;
                mainMsg.setText("Try again?");
                button1.setVisibility(View.VISIBLE); // This is where they first become visible, here the code works for me.
                button2.setVisibility(View.VISIBLE);
                button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mRequestingLocationUpdates = false;
                        proceed = true;
                        count = 0;
                        button1.setVisibility(View.GONE);
                        button2.setVisibility(View.GONE);

                        /* Here the code gets crazy! Those couple lines above won't work.
                        I press button2 one time and the two button's fail to disappear.
                        Chronometer goes to 0 and starts again. The app will try to find a location again.
                        If I press button2 a second time, while the chronometer is still counting,
                        then the buttons disappear and the chronometer resets and start over, as originally intended.
                        ANOTHER INFO: if I remove the three lines above the button methods, it will work, but the app won't function as intended.*/

                        togglePeriodicLocationUpdates(); // This code calls a method to control whether the app will start looking for a location or shut the location services.
                    }
                });
                button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mainMsg.setText("Continue without a defined location.");
                        button1.setVisibility(View.GONE); // Here the code works no matter what...
                        button2.setText("Continue");
                        button2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                finish();
                            }
                        });
                    }
                });

            }
        }
    });
    mainMsg = (TextView) findViewById(R.id.mainMsg);

    // Check availability of google play services
    if (checkPlayServices()) {

        buildGoogleApiClient();

        createLocationRequest();

    }

    new Timer().schedule(new TimerTask() { // This launches the location finder on timer, so that the user doesn't need to press anything to begin location retrieval
        @Override
        public void run() {
            runOnUiThread(new Thread(new Runnable() {
                @Override
                public void run() {
                    togglePeriodicLocationUpdates();
                }
            }));
        }
    }, 2000);

}

private void togglePeriodicLocationUpdates() {
    if (!mRequestingLocationUpdates) { // Begin search cycle
        // Changing the button text
        if (count == 0) {
            mainMsg.setText("Getting location...");
            c.setBase(SystemClock.elapsedRealtime());
        }

        mRequestingLocationUpdates = true;

        // Starting the location updates
        startLocationUpdates();
        c.start();
        Log.d(TAG, "Periodic location updates started!");

    } else { // End search cycle

        mRequestingLocationUpdates = false;
        if (count == -1) {
            c.stop();
        }
        // Stopping the location updates
        stopLocationUpdates();
        if (proceed & count > 0) {
            togglePeriodicLocationUpdates();
        }

        Log.d(TAG, "Periodic location updates stopped!");
    }
}

@Override
public void onLocationChanged(Location location) { // Listener for locations get by the google play location service

    if (mLastLocation == null) mLastLocation = location; // checks if first time
    try {
        if (location.getLatitude() != mLastLocation.getLatitude() &
                location.getLongitude() != mLastLocation.getLongitude()) {

            /* Seems the location API can the last known location instead of the ACTUAL location.
            I didn't like that too much, so I verify if the freshly-collected location is the same as the "last known".
            If the location is new.......*/

            c.stop(); // Halt the chronometer
            stopLocationUpdates(); // Halt location services

            // Assign the new location
            mLastLocation = location;
            this.location = location;

            Toast.makeText(getApplicationContext(), "Location changed!", Toast.LENGTH_SHORT).show();

            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            mRequestingLocationUpdates = false;

            // This is supposed to be the last snippet of location code to run...

        } else {

            // This should run if the location found is not an actual update on your position.
            // For your information, this runs in cycles, half dozen or so if the (GPRS) signal is poor.

            mainMsg.setText("Text to keep user entertained...");
            if (!proceed) {
                proceed = !proceed;
            }
            count++; // Variable to count the number of cycles run
            togglePeriodicLocationUpdates(); // Call the toggle function again to run another cycle
            // Running those cycles seemed to help my GPS to find a location, instead of just leaving it on all the time.
        }
    } catch (NullPointerException e) {
        c.stop();
        e.printStackTrace();
    }
}

我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center_vertical|center_horizontal"
          android:orientation="vertical"
          tools:context="br.com.gerpaudit.activity.LocationRetrieverActivity"
          android:paddingTop="20sp"
          android:paddingBottom="20sp">

<TextView
    android:id="@+id/mainMsg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:text="O aplicativo irá buscar sua localização, aguarde."
    android:gravity="center_vertical|center_horizontal"/>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Tempo decorrido: "
        android:id="@+id/textView"
        android:textStyle="bold"
        android:textSize="25sp"/>

    <Chronometer
        android:id="@+id/crono"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:allowUndo="false"
        android:textSize="25sp"
        android:textStyle="bold"
        />
</LinearLayout>

<LinearLayout
    android:id="@+id/loc_btn_layout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <Button
        android:id="@+id/btn_loc_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        android:background="@drawable/bg_voltar"
        android:text="Não"
        android:layout_weight="1"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:visibility="visible"/>

    <Button
        android:id="@+id/btn_loc_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        android:background="@drawable/bg_voltar"
        android:text="Sim"
        android:layout_weight="1"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:visibility="visible"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

尝试删除

button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRequestingLocationUpdates = false;
                    proceed = true;
                    count = 0;
                    button1.setVisibility(View.GONE);
                    button2.setVisibility(View.GONE);

                    /* Here the code gets crazy! Those couple lines above won't work.
                    I press button2 one time and the two button's fail to disappear.
                    Chronometer goes to 0 and starts again. The app will try to find a location again.
                    If I press button2 a second time, while the chronometer is still counting,
                    then the buttons disappear and the chronometer resets and start over, as originally intended.
                    ANOTHER INFO: if I remove the three lines above the button methods, it will work, but the app won't function as intended.*/

                    togglePeriodicLocationUpdates(); // This code calls a method to control whether the app will start looking for a location or shut the location services.
                }
            });
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mainMsg.setText("Continue without a defined location.");
                    button1.setVisibility(View.GONE); // Here the code works no matter what...
                    button2.setText("Continue");
                    button2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                }
            });

if (c.getText().equals("00:05"))语句之后,将它们放在

之后
button1 = (Button) findViewById(R.id.btn_loc_1);
button1.setVisibility(View.GONE);
button2 = (Button) findViewById(R.id.btn_loc_2);
button2.setVisibility(View.GONE);

答案 1 :(得分:0)

原来我需要在更改这些视图的状态之前重置计时器。奇怪的是,我验证了按钮的状态,它们的可见性显示为“8”,即“View.GONE”,但仍然显示。现在我添加了一行来重置Chronometer,然后再将它们设置为GONE,它可以工作。不要问我为什么。