我對android開發非常陌生,但知道一點Java。我有問題知道在我的代碼中放置Toast.setGravity(Gravity.TOP,0,0)的位置。非靜態setGravity不能從靜態上下文中引用
下面的代碼:
package ca.ignitestudio.geoquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
Toast.setGravity(Gravity.TOP,0,0);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
}
}
你可以標記'setGravity'爲'static'例如'公共靜態setGravity(..)'? –
不,我不能,因爲如果我這樣做會給我更多的錯誤。 –
那麼makeText是如何工作的呢? –