老罗视频学习笔记。
一.Button控件。
1)button焦点变化
基本用法就不多说了,主要记录下Button按钮焦点变化和图文混排的功能。
在之前TextView和EditText的项目中新添加一个menu选项,点击后弹出新的Activity,在新的ButtonActivity里实现功能。
首先继承OnClickListener,OnTouchListener,OnFocusChangeListener, OnKeyListener。注意包不要导错,有时系统自动导入的包是错误的,要手动修改下,导入
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
在onCreate方法里获取控件对象,并且设置监听函数。
commonButton = (Button)findViewById(R.id.commonbutton);
imageButton = (Button)findViewById(R.id.imagebutton);
commonButton.setOnClickListener(this);
imageButton.setOnClickListener(this);
imageButton.setOnTouchListener(this);
imageButton.setOnFocusChangeListener(this);
imageButton.setOnKeyListener((android.view.View.OnKeyListener) this);
当控件被点击时,会调用OnClick回调函数。实现控件变大变小。
//当界面中的按钮被触发时会调用这个事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button button = (Button)v;
//当当前按钮的宽度达到窗体的宽度的时候
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);
}
当触摸屏被触摸时,会调用onTouch方法。实现图片的变化。
//表示显示屏被触摸的时候会回调这个方法,手指,触摸笔,按键,滚动条等
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_UP){
v.setBackgroundResource(R.drawable.button1);
}else if (event.getAction()==MotionEvent.ACTION_DOWN) {
v.setBackgroundResource(R.drawable.button2);
}
return false;
}
当焦点变化时,会调用onFocusChange这个回调方法。改变图片。
//当前控件的焦点发烧变化时
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
imageButton.setBackgroundResource(R.drawable.button2);
}else {
imageButton.setBackgroundResource(R.drawable.button1);
}
}
当使用按键时,会调用onKey回调方法。改变图片。
//当产生一个key的事件是会回调这个事件
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(KeyEvent.ACTION_DOWN == event.getAction()) {
v.setBackgroundResource(R.drawable.button3);
}else if (KeyEvent.ACTION_UP == event.getAction()) {
v.setBackgroundResource(R.drawable.button2);
}
return false;
}
效果图如下:
2)button图文混杂。
可以静态xml文件中设置,也可以在程序中动态设置。
静态设置xml文件如下,文字的上下左右都有图片。
<Button
android:id="@+id/imagetextbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/star"
android:text="静态图文混排"
android:textSize="10sp"
android:drawableLeft="@drawable/star"
android:drawableRight="@drawable/star"
android:drawableBottom="@drawable/star"></Button>
动态设置那么ButtonActivity.java内容如下:
imagetextButton = (Button)findViewById(R.id.imagetextbutton2);
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);
imagetextButton.setTextSize(20);
imagetextButton.append(spannableStringLeft);
imagetextButton.append("动态图文混合");
imagetextButton.append(spannableStringRight);
效果图如下:
二.RadioButton的使用,如果需要几个RadioButton是互斥的,即每次只能选中一个的话,要用到RadioGroup。
布局文件如下:
在RadioGroup中放两个RadioButton,用来选择性别是男是女。
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="性别"></TextView>
<RadioGroup
android:id="@+id/RadioGroupSex"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/RadioButtonSexBoy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton android:id="@+id/RadioButtonSexGirl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<Button
android:id="@+id/buttonTip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你选择了" />
</LinearLayout>
在ButtonActivity.java中。
radioGroup = (RadioGroup)findViewById(R.id.RadioGroupSex);
ButtonTip = (Button)findViewById(R.id.buttonTip);
ButtonTip.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int len = radioGroup.getChildCount();
String mesString = "";
for(int i = 0;i<len;i++){
RadioButton radioButton = (RadioButton)radioGroup.getChildAt(i);
if(radioButton.isChecked()){
mesString = radioButton.getText().toString();
}
}
Toast.makeText(ButtonActivity.this, mesString, 1).show();
}
});
选择男,点击按钮,会提示“男”。
效果图如下:
三.ToggleButton的使用。
这个是开关控件。
布局文件如下:中间嵌套的LinaerLayout控件是两个button按钮,通过操作togglebutton来改变两个button的排列方式。
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOn="纵向排列"
android:textOff="横向排列"
android:layout_gravity="center"></ToggleButton>
<LinearLayout
android:id="@+id/mylayout"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"></Button>
</LinearLayout>
</LinearLayout>
代码如下:
//ToggleButton控件
toggleButton = (ToggleButton)findViewById(R.id.togglebutton);
final LinearLayout myLinearLayout = (LinearLayout)findViewById(R.id.mylayout);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
myLinearLayout.setOrientation(1);//表示垂直布局
}else {
myLinearLayout.setOrientation(0);//表示水平布局
}
}
});
效果图如下: