gpt4 book ai didi

android - 为什么 onClickListener 在 onCreate 方法之外不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:35 24 4
gpt4 key购买 nike

<分区>

当我尝试对按钮使用 onClickListener 方法时,在任何 onCreate 或 onPause 或 onAnything 方法之外的变量,它不起作用。我什至无法在“onAnything”方法之外设置按钮变量的值。帮助会很棒。

谢谢!

public class StartingPoint extends Activity {
/** Called when the activity is first created. */

int counter;
Button add= (Button) findViewById(R.id.bAdd);
Button sub= (Button) findViewById(R.id.bSub);
TextView display= (TextView) findViewById(R.id.tvDisplay);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
Log.i("phase", "on create");
counter=0;

add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText(""+counter);
display.setTextSize(counter);
Log.i("phase", "add");
}
});
sub.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText(""+counter);
display.setTextSize(counter);
display.setTextColor(Color.GREEN);
Log.i("phase", "sub");
}
});

}

@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("phase", "on start");
SharedPreferences prefs = getPreferences(0);
int getfromfile = prefs.getInt("counter_store", 1);
counter=getfromfile;
display.setText(""+getfromfile);
display.setTextSize(getfromfile);
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("phase", "on stop");
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putInt("counter_store", counter);
editor.commit();
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
counter=0;
Log.i("phase", "on destroy");

}

}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com