傢伙,我需要這個代碼幫助:的Android SDK如何OnClickListener
public View.OnClickListener btnGetLastLocationOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected()) {
getMyLocation();
} else {
Toast.makeText(AddEditActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(AddEditActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
};
所以我想要做的是當我點擊btnGetLocation此代碼應工作,但不是代碼自動工作,當我啓動活動,所以只有當我點擊按鈕時,我如何才能做作品?
我希望你們明白這個問題,我是新的Android開發者,所以我仍然在學習原諒我的愚蠢問題。
謝謝
***更新:
這裏是整個代碼:
TextView txtmylink;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Button getlastlocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtmylink = (TextView) findViewById(R.id.mylink);
getlastlocation = (Button) findViewById(R.id.getlastlocation);
getlastlocation.setOnClickListener(getlastlocationOnClickListener);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
View.OnClickListener getlastlocationOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected()) {
getMyLocation();
} else {
Toast.makeText(MainActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
};
private void getMyLocation() {
try {
/* code should explicitly check to see if permission is available
(with 'checkPermission') or explicitly handle a potential 'SecurityException'
*/
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
txtmylink.setText(String.valueOf("http://maps.google.com/maps?q=") +
String.valueOf(mLastLocation.getLatitude()) +
String.valueOf(",")+ String.valueOf(mLastLocation.getLongitude()));
} else {
Toast.makeText(MainActivity.this,
"mLastLocation == null",
Toast.LENGTH_LONG).show();
}
} catch (SecurityException e) {
Toast.makeText(MainActivity.this,
"SecurityException:\n" + e.toString(),
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
getMyLocation();
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(MainActivity.this,
"onConnectionSuspended: " + String.valueOf(i),
Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(MainActivity.this,
"onConnectionFailed: \n" + connectionResult.toString(),
Toast.LENGTH_LONG).show();
}
}
請發表您的滿級代碼。 – Rashin