如何停止android编辑文本框刷新

时间:2014-03-26 15:09:22

标签: android

我是Android的新手,我试着获取我当前的位置地址。我的代码正常工作,但文本框的值是时不时刷新。我怎么阻止它......

这是我的代码..

公共类MylocMainActivity扩展了Activity {

 String lat="", lon="";
    String ret= "" ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myloc_main);


      //button click done
    Button btnLocation = (Button)findViewById(R.id.btn_done);
    btnLocation.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            location(); 
            Log.d("OnClick","Passed");
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.myloc_main, menu);
    return true;
}


 public void location()
    {
        // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) MylocMainActivity.this.getSystemService(Context.LOCATION_SERVICE);
        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                lat = Double.toString(location.getLatitude());
                lon = Double.toString(location.getLongitude());
                TextView tv = (TextView) findViewById(R.id.tv_location);
                tv.setText("Your Location is:" + lat + "--" + lon);
                tv.setText(GetAddress(lat, lon));
                Intent i = new Intent(MylocMainActivity.this,MainActivity.class);
                i.putExtra("clist",ret.toString());
                startActivity(i);

            }


            public void onStatusChanged(String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
        };
        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

    }


    // get the address 
    public String GetAddress(String lat, String lon)
    {
        Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

        try {
            List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lon), 1);
            if(addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                }
                ret = strReturnedAddress.toString();
            }
            else{
                ret = "No Address returned!";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ret = "Can't get Address!";
        }
        return ret;
    }

}

........主要活动类.............

公共类MainActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent startbuttonintent = getIntent();

    String conlist = null;

    conlist = startbuttonintent.getStringExtra("clist");
    TextView name = (TextView) findViewById(R.id.family_Text);
    name.setText(conlist);



     Button btn = (Button)findViewById(R.id.btn_location);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(MainActivity.this,MylocMainActivity.class);
                startActivity(i);
                // TODO Auto-generated method stub

            }
        });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:0)

每次更改位置时,都会更改textview的内容,尽管由于数字精度,内容可能实际上没有更改。 尝试使用text或类似设置文本视图:

public void onLocationChanged(Location location) 
{
    // Called when a new location is found by the network location provider.
    lat = Double.toString(location.getLatitude());
    lon = Double.toString(location.getLongitude());
    TextView tv = (TextView) findViewById(R.id.tv_location);
    if(!tv.getText().equals(GetAddress(lat, lon)))
    {
        tv.setText("Your Location is:" + lat + "--" + lon);
        tv.setText(GetAddress(lat, lon));
    }
    Intent i = new Intent(MylocMainActivity.this,MainActivity.class);
    i.putExtra("clist",ret.toString());
    startActivity(i);
}

答案 1 :(得分:0)

您可以将位置从onLocationChanged存储在位置值(mLocation)中。 每当位置发生变化时,请检查

if (mLocation != null) { // do something }