㈠ android Application全局变量
不是啊,你声明在类里面而不是onCreate方法里面就可以在这个Activity中使用。
public class GuessNumberActivity extends Activity {
Button btn1 = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.btn1);
}
//在其他函数中使用
bt1.setOnClickListener(new Button.onClickListener(){.........});
㈡ android变量声明问题
正确的作为是吧 ButtonClickListener 定义为类CheckBoxActivity里的一个成员变量
OnClickListener buttonClickListener = new OnClickListener(){
public void onClick(View arg0){
String str="";
if(checkbox1.isChecked())
str=str+checkbox1.getText();
if(checkbox2.isChecked())
str=str+checkbox2.getText();
if(checkbox3.isChecked())
str=str+checkbox3.getText();
if(checkbox4.isChecked())
str=str+checkbox4.getText();
Toast.makeText(CheckBoxActivity.this, str+"被选择",Toast.LENGTH_LONG).show();
}
};
㈢ android怎么声明一个变量2个方法都可以访问
申明全局的啊
定义在类里面啊
java">{
privateEdittextet;
.
.
.
publicvoidonCreate(BundlesaveInstance){
.
.
.
}
}
㈣ javaAndroid开发,如何定义全局变量
自定义一个类继承Application,fontFace作为一个静态变量放在Application里,重写自定义Application类的onCreate方法,在里面初始化fontFace变量,最后记得在AndroidManifest里注册自定义的Application类
引用的时候用Application类名.fontFace就可以了
㈤ android 全局变量怎么使用
全局变量怎么使用?说法太模糊了,如果你是要声明一个全局变量,假设有一个类,你只要在类成员变量声明即可,例如:我声明一个int 类型的变量为10:private int number=10; 你可以在该类中方法内调用它(比如用来做加减乘除),也可以重新赋值给它(number=XX, XX必须是int类型,否则可能需要强转)。
㈥ 为什么android的成员变量要定义final才能在方法里面用
因为你这个变量ll是onCreate()方法的一个局部变量,而在onClick()方法里面的new onClickListener(){}其实是以onClickListener接口为基础隐式创建了一个主类的内部类,也就是说new onClickListener(){}中括号内的东西,和oncreate()方法的作用域不同。所以如果你要跨作用域使用的话,方法有两种:一种就是你看到的在局部作用域内将属性声明为final的,第二种就是把你的方法属性的声明放到方法外作为一个类属性,这个时候由于内部类是在主类里边的,所以它可以访问主类的全部属性。
修正一下:两种方法都不是跨作用域使用,而是用不同的方法将ll变量的作用域扩大。
㈦ android 如何定义全局变量
找到一个和我有类似需求的问题,其下给出了不错的解决方案,也正是我之前想到的,这种方法貌似很方便。 The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context. --如想在整个应用中使用,在java中一般是使用静态变量,而在android中有个更优雅的方式是使用Application context。 As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application. --每个Activity 都是Context,其包含了其运行时的一些状态,android保证了其是single instance的。 The way to do this is to create your own subclass of android.app.Application,and then specify that class in the application tag in your manifest.Now Android will automatically create an instance of that class andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect): --方法是创建一个属于你自己的android.app.Application的子类,然后在manifest中申明一下这个类,这是 android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。
㈧ 在android里面怎么声明,定义一个叫orientation的变量
android:orientation是排列方向的意思。是指VIEW的排列方向你可以选择vertical即垂直排列,也可以选择水平horizontal
㈨ Android中声明变量方式的区别
前面是赋空值,后面的是没有初始化,简单来说,区别就是一个已经初始化,一个还未初始化。
一般来讲,除了常量,初始化部分要放在构造函数里面,并且要鲜明,清晰,这样一个是为了在以后的调用过程中防止未赋值的情况(例如你赋值了空值,可是在调用前你并没有给它一个有意义的值,这样就容易出错,而又不好找原因),另外一个呢就是为了结构清晰和节约空间。1楼所说的默认构造函数被改写,就是重写构造函数,给构造函数添加参数,以满足程序的要求。