我有类 MainActivity
public class MainActivity extends Activity {
int mVariable= ((Constant)getApplicationContext()).variable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button count= (Button)findViewById(R.id.button1);
final TextView tx = (TextView)findViewById(R.id.text);
count.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
mVariable ++;
tx.setText(" "+mVariable);
....
和常量类
public class Constant extends Application {
public int variable=0;
}
这不起作用..我在清单中声明常量。
<application
...
android:name=".Constant"/>
我想像这段代码一样通过 onClick 设置值并更改全局变量的值.. 谢谢