是否有可能在Android上onCreate()
方法之前创建变量?我曾尝试过,但无济于事,所以我假设你不能,但我只是想仔细检查。究其原因就是因为我想在我的TextWatcher
充当计数器来访问一个变量,但它超出范围,并要求对我来说,使它最终,这显然行不通的,因为其作为一个计数器,并需要增加。我重视我下面的代码:Android在OnCreate之前声明变量?
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText answerText = (EditText) findViewById(R.id.answer);
final TextView text = (TextView) findViewById(R.id.wordtoanswer);
final ArrayList updatedList = helperMethod();
text.setText(updatedList.get(0).toString());
final String wordFinal = updatedList.get(0).toString();
while(true)
{
answerText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
System.out.println(counter);
String answer = s.toString();
if(answer.equals(wordFinal))
{
text.setText(updatedList.get(counter).toString());
answerText.setText("");
}
}
});
counter++;
}
}
希望你们明白,为什么我想在onCreate()
之前声明它,因为只有这样,才能TextWatcher
里面的方法其实没有他们被淘汰的范围内访问它。无论如何,我可以解决这个问题吗?请让我知道如果你需要更多的信息/代码!
将改变它为int计数器前上创建,然后在它intializing至0 OnCreate方法使其工作?我试图测试它,但有问题连接Android手机日食! – user2677095 2014-09-02 22:28:44