2016-07-15 69 views
1

傢伙,我需要這個代碼幫助:的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(); 
} 

}

+1

請發表您的滿級代碼。 – Rashin

回答

1

試試這個:

public void onClick(View v) { 
      if (view.getId() == R.id.btnGetLocation) 
      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(); 
       } 
     } 

如果仍沒有工作寫你的代碼是這樣的:

你的活動mu ST實現OnClickListener和

btnGetLocation.setOnClickListener(this) 

,你將擁有的onClick這樣

public void onClick(View v) { 
       if (view.getId() == R.id.btnGetLocation) 
       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(); 
        } 
      } 
+1

謝謝你,但我仍然有同樣的問題,這是一旦我開始活動的價值得到加載,如果你願意可以幫助我會感激它。 –

+0

你有沒有使用這個提示:btnGetLocation.setOnClickListener(this)...? – aurelianr

+0

是的,但我得到(此)的紅線,只是要清除此代碼 btnGetLocation。setOnClickListener(this) 應該在這個void 保護無效的onCreate(Bundle savedInstanceState –

2

那是因爲你還沒有相關的,您對View.onClickListener對象寫入任何東西呢。因此,當活動開始時,監聽器的代碼被執行。當你想要一個特定的視圖'A'來響應點擊時,你可以將它與View.onClickListener聯繫起來。所以在你的情況下添加以下內容。

yourbutton.setOnClickListener(btnGetLastLocationOnClickListener) 

確保你已經綁定的yourbuttonButton視圖。

+0

工作100%謝謝男人,現在按鈕正在工作,但仍然當我開始活動時自動獲得的價值,並把它放在文本框中,當我點擊按鈕它做同樣的事情,所以我只想按鈕獲得價值,如果你能幫忙,我會非常高興。 –

0

您可以用下面的代碼

btnGetLocation = (Button) findViewById(R.id.btnGetLocation); // or whatever you called the button 
btnGetLocation.setOnClickListener(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(); 
      } 
    } 
} 

注意,您可以在onCreate()方法添加以下代碼,使完成它,它的工作

+0

它沒有工作,但謝謝任何方式的人。 –

+0

@ rooot_999請傳遞您的代碼 –

+0

@not_again_stackoverflow的答案適用於我,但仍然在我開始活動時自動獲取該值並將其放入文本框中,並且當我單擊該按鈕時它也執行相同的操作,所以我只想要按鈕來獲得價值,如果你能幫忙,我會更高興 –