wordpress新网站百度大搜是什么
我将Android控件的Button和Checkbox的学习知识总结一下和大家共享
在Android开发中,按钮和复选框是非常常用的控件
Button控件的基本使用方法很简单,在布局文件中使用<Button>既可以了,或者在java代码:Button button = (Button)findViewById(R.id.button1);
Checkbox也是一样的。
1、Button控件
按钮控件的最常见的是单击事件,可以通过Button.setOnClickListener方法设置处理单机事件的监听器,也可以在<Button>标签的android:onClick属性指定单击事件方法名。如果当前类实现了android.view.view.OnClickListener接口,可以直接将this传入到setOnClickListener方法。
下面看例子:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center_horizontal"><Button android:id="@+id/btnCommonButton" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="我的按钮1" /><Button android:id="@+id/btnImageButton" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="按钮"android:gravity="center" android:background="@drawable/button1" />
</LinearLayout>
java实现文件:
public class Main extends Activity implements OnClickListener, OnTouchListener,OnFocusChangeListener, OnKeyListener
{private int value = 1;private Button imageButton;private Button commonButton;private View lastFocusview;@Overridepublic void onClick(View view){Button button = (Button) view;if (value == 1&& button.getWidth() == getWindowManager().getDefaultDisplay().getWidth())value = -1;else if (value == -1 && button.getWidth() < 100)value = 1;button.setWidth(button.getWidth() + (int) (button.getWidth() * 0.1)* value);button.setHeight(button.getHeight() + (int) (button.getHeight() * 0.1)* value);}@Overridepublic boolean onKey(View view, int keyCode, KeyEvent event){ if (KeyEvent.ACTION_DOWN == event.getAction()){view.setBackgroundResource(R.drawable.button3);}else if (KeyEvent.ACTION_UP == event.getAction()){view.setBackgroundResource(R.drawable.button2);}return false;}@Overridepublic void onFocusChange(View view, boolean hasFocus){if (hasFocus){imageButton.setBackgroundResource(R.drawable.button2);}else{imageButton.setBackgroundResource(R.drawable.button1);}}@Overridepublic boolean onTouch(View view, MotionEvent event){if (event.getAction() == MotionEvent.ACTION_UP){view.setBackgroundResource(R.drawable.button1);}else if (event.getAction() == MotionEvent.ACTION_DOWN){view.setBackgroundResource(R.drawable.button2);}return false;}@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);commonButton = (Button) findViewById(R.id.btnCommonButton);imageButton = (Button) findViewById(R.id.btnImageButton);commonButton.setOnClickListener(this);imageButton.setOnClickListener(this);imageButton.setOnTouchListener(this);imageButton.setOnFocusChangeListener(this);imageButton.setOnKeyListener(this);}
}
也可以试试图文混排的按钮:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="120dp"><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:drawableTop="@drawable/star" android:text="按钮1" /><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:drawableBottom="@drawable/star"android:text="按钮2" android:drawablePadding="30dp" /><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:drawableLeft="@drawable/star"android:text="按钮3" /><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:drawableRight="@drawable/star"android:text="按钮4" android:drawablePadding="20dp" /></LinearLayout><Button android:id="@+id/button" android:layout_width="200dp"android:layout_height="200dp" android:layout_marginTop="10dp" />
</LinearLayout>
java实现文件:
public class Main extends Activity
{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button = (Button) findViewById(R.id.button);SpannableString spannableStringLeft = new SpannableString("left");Bitmap bitmapLeft = BitmapFactory.decodeResource(getResources(),R.drawable.image_left);ImageSpan imageSpanLeft = new ImageSpan(bitmapLeft, DynamicDrawableSpan.ALIGN_BOTTOM);spannableStringLeft.setSpan(imageSpanLeft, 0, 4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);SpannableString spannableStringRight = new SpannableString("right");Bitmap bitmapRight = BitmapFactory.decodeResource(getResources(),R.drawable.image_right);ImageSpan imageSpanRight = new ImageSpan(bitmapRight);spannableStringRight.setSpan(imageSpanRight, 0, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);button.append(spannableStringLeft);button.append("我的按钮");button.append(spannableStringRight);}
}
接下来介绍一下ImageButton图片按钮,其实和普通按钮一样,差别在与不用文本显示,而是图片:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageButton android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/button1_1" /><ImageButton android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/button2_1" />
</LinearLayout>
这边补充介绍RadioButton选项按钮控件和ToggleButton开关状态按钮控件
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><RadioGroup android:layout_width="wrap_content"android:layout_height="wrap_content"><RadioButton android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="选项1" /><RadioButton android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="选项2" /><RadioButton android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="选项3"android:drawableLeft="@drawable/star" android:drawableTop="@drawable/circle"android:drawableRight="@drawable/star" android:drawableBottom="@drawable/circle" android:drawablePadding="20dp" /></RadioGroup>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="fill_parent"><ToggleButton android:layout_width="wrap_content"android:layout_height="wrap_content"/><ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_marginLeft="30dp"android:textOff="打开电灯" android:textOn="关闭电灯" />
</LinearLayout>
2、Checkbox控件
Checkbox复选框控件,在这里采用动态加载的方式,在xml布局文件的根节点不仅可以像<LinearLayout>,<RelativeLayout>一样的ViewGroup,也可以直接使用View,下面采用Checkbox标签。
Checkbox.xml;
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/checkbox" android:layout_width="fill_parent"android:layout_height="wrap_content" />
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/button" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="确定" />
</LinearLayout>
java文件:
public class Main extends Activity implements OnClickListener
{private List<CheckBox> checkboxs = new ArrayList<CheckBox>();@Overridepublic void onClick(View view){String s = "";for (CheckBox checkbox : checkboxs){if (checkbox.isChecked())s += checkbox.getText() + "\n";}if ("".equals(s))s = "您还没选呢!";new AlertDialog.Builder(this).setMessage(s).setPositiveButton("关闭", null).show();}@Overridepublic void onCreate(Bundle savedInstanceState){String[] checkboxText = new String[]{ "是学生吗?", "是否喜欢Android?", "买车了吗?", "打算出国吗?" };super.onCreate(savedInstanceState);LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);for (int i = 0; i < checkboxText.length; i++){CheckBox checkbox = (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);checkboxs.add(checkbox);checkboxs.get(i).setText(checkboxText[i]);linearLayout.addView(checkbox, i);}setContentView(linearLayout);Button button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}
}