Android按钮onClick只能运行一次

时间:2011-10-27 16:53:01

标签: android

我试图创建一个应用程序,它会占用手机与gps一起移动的距离。我的问题是,当我点击按钮b1时,这个监听器不会停止收听,也不会再进一步​​点击。

我做错了什么?         包com.HowMuchHowFar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.location.*;

public class HowMuchHowFarActivity extends Activity {
//declare variables
double lastlon,lastlat,curlon,curlat=0.0;
float total_distance=0; 
float dist[]= new float[1];
boolean k2m_check=true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //create button
    Button b1=(Button) findViewById(R.id.firstbtn);

    System.out.println("hhhh");

    //Open start activity
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //change to next activity
            setContentView(R.layout.start);

            //Create kilometer to meter button
            Button k2m=(Button) findViewById(R.id.Kilometer);

            //get access to text field
            TextView display = (TextView) findViewById(R.id.display);

            //start location manager
            LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE); 
            lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
            Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

            //Prepare for null value and force close
            if(loc==null){
                display.setText("No GPS location found");
                }
                else{
                    //set Current latitude and longitude
                    curlon=Double.valueOf((String.valueOf(loc.getLongitude())));
                    curlat=Double.valueOf((String.valueOf(loc.getLatitude())));

                    }
            //Set the last latitude and longitude
            lastlon=curlon;
            lastlat=curlat;

            //Start kilometer to meter button listener

            k2m.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //start text check
                      //create button 
                    Button k2m1=(Button) findViewById(R.id.Kilometer);

                    //get access to text field
                    TextView disp1 = (TextView) findViewById(R.id.display);

                    if(k2m_check==true){
                        //Change text
                        k2m1.setText("Meters");
                        onDestroy();

                        //startActivity();
                        //Set boolean for the next change
                        k2m_check=false;

                        disp1.setText(total_distance/1000+" Kilometers");

                    }

                    if(k2m_check==false){
                        //Change text
                        k2m1.setText("Kilometers");

                        //Set boolean for next change
                        k2m_check=true;

                        //Set display
                        disp1.setText(total_distance*1000+" Meters");
                    }//end text check

                }
            });//end k2m setonclicklistener


        }
    });


}//ends on create
LocationListener Loclist=new LocationListener(){

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        //start location manager
        LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE); 
        TextView display1 = (TextView) findViewById(R.id.display);

        //Create display for kilometer cost
        TextView display_cpk = (TextView) findViewById(R.id.display_cpk);
        TextView display_totalcost = (TextView) findViewById(R.id.display_totalcost);

        //Get last location
        Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);            

        //Request new location
        lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);

        //Get new location
        Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);

        //get the current lat and long
        curlon=Double.valueOf((String.valueOf(loc.getLongitude())));
        curlat=Double.valueOf((String.valueOf(loc.getLatitude())));

        //Get distance between 2 locations put the distance into dist[0], in meters
        Location.distanceBetween(lastlat,lastlon,curlat,curlon,dist);

        //add the distance from this update to the total distance
        total_distance=dist[0]+total_distance;

        //Show distance travelled           
        display1.setText("Total distance "+total_distance+" Meters");

        //Get cost per kilometer
        CharSequence h=display_cpk.getText();
        double mul=Double.parseDouble(h.toString());

        //perform multipication and output cost
        double cost=mul*(total_distance/1000);
        display_totalcost.setText(String.valueOf(cost));
        System.out.println(dist[0]);
        System.out.println(mul);
        System.out.println(total_distance/1000);


    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status,
            Bundle extras) {
        // TODO Auto-generated method stub

    }

};

}

1 个答案:

答案 0 :(得分:3)

这是因为您正在更改内容视图,这意味着之前布局中的任何按钮现在都无效。如果您要发布活动,则应使用startActivity()startActivityForResult()而不是更改内容视图。在所有(大多数?可能有例外)的情况下,您应该每次活动只调用一次setContentView()