
在Android的API中可以发现有很多用整数集来作为参数的地方,先来看一下实例。 LinearLayout是大家所熟知的一个UI基本元素,它里面有一个方向的属性,可以通过以下方法来设置: LinearLayout.setOrientation(int); 使用的时候,通常都是这样: LinearLayout.s
在Android的API中可以发现有很多用整数集来作为参数的地方,先来看一下实例。LinearLayout.setOrientation(int);
LinearLayout.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.setOrientation(0); // LinearLayout.HORIZONTAL = 0 LinearLayout.setOrientation(1); // LinearLayout.VERTICAL = 0x01
LinearLayout.setOrientation(Integer.MAX_VALUE); LinearLayout.setOrientation(Integer.MIN_VALUE); LinearLayout.setOrientation(2012);
LinearLayout.setOrientation(0); LinearLayout.setOrientation(1);
因为XML布局会在编译时被处理,如果有非法的值,会有编译错误的。我想这也就是Android特别鼓励开发者用XML来制作所有的布局的一个原因吧。
实例,三个没有设置指向的线性布局,默认是水平放置,在代码中设置了几个离谱的值,发现它们还是水平的,也就是说设置离谱的值不会出错,但也不起作用:
运行结果如下:

代码如下:
package com.android.explorer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class LinearLayoutTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linearlayout_test);
LinearLayout one = (LinearLayout) findViewById(R.id.linearlayout_test_1);
one.setOrientation(2012);
LinearLayout two = (LinearLayout) findViewById(R.id.linearlayout_test_2);
two.setOrientation(Integer.MAX_VALUE);
LinearLayout three = (LinearLayout) findViewById(R.id.linearlayout_test_3);
three.setOrientation(Integer.MIN_VALUE);
}
}public class LinearLayout extends ViewGroup {
private Orientation mOrientation;
public enum Orientation {
HORIZONTAL, VERTICAL
};
public void setOrientation(Orientation dir) {
mOrientation = dir;
}
}import android.widget.LinearLayout; LinearLayout.setOrientation(Orientation.HORIZONTAL); LinearLayout.setOrientation(Orientation.VERTICAL);
Button.setEnabled(true); // enable the button Button.setEnabled(false); // disable the button
// com/android/mms/data/ContactList.java public String[] getNumbers(boolean);
String[] mms = getNumbers(true); String[] sms = getNumbers(false);
Button.setEnabled(true); // enable the button Button.setEnabled(false); // disable the button
Button.enable(); Button.disable();
// com/android/mms/data/ContactList.java public String[] getNumbersForSms(); public String[] getNumbersForMms();
// com/android/mms/data/ContactList.java
public String[] getNumbersForSms() {
return getNumbers(false);
}
public String[] getNumbersForMms() {
return getNumbers(true);
}
private String[] getNumbers(boolean) {
// implementation
}