我是新來的android,我試圖讓我當前的位置address.my代碼工作,但文本框的值是刷新時間。我如何阻止它......如何停止android編輯文本框刷新
這是我的代碼..
公共類MylocMainActivity延伸活動{
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擴展活動{
@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;
}
}
你的TextView可能是因爲刷新的位置發生了變化,顯着與否。看到我的答案。 –