安卓基础控件

TextView

文本显示为主要用途的控件。除了基本的宽高位置背景基本属性外,主要是文本相关的属性。
1. 文字,常用方法。

 android:text="string"//可以是字符串,也可以是字符串的引用(values文件中对应的值,如android:text="@string/app_name")。

 public final void setText(@StringRes int resid)//id的引用形式为,R.string.app_name 值存在于values文件中,形式为:

 <resources>
    <string name="app_name">appname</string>
</resources>

public final void setText(CharSequence text)//参数直接设置字符串。例如:"中国队加油!" 

2.文字颜色,常用方法。

android:textColor="#11111111"//同文字,除了直接设置16进制的数值外,还接收引用:android:textColor="@color/colorAccent"

public void setTextColor(@ColorInt int color)
//直接设置16进制数值。
public void setTextColor(ColorStateList colors)
//配合选择器来设置颜色。比如获取焦点时,被touch时等状态下的颜色值。

3.设置背景。

 android:background=""   //值可以是颜色值可以是样式也可以是图片,统称Drawable。 
 public void setBackground(Drawable background)。

Button

作为TextView的子类除了有父类的属性,最终要的就是可以点击(虽然textview也可以点击)和相应事件。

1.在xml中制定方法,在代码中写响应事件。不推荐

android:onClick="onBtnClick"
// 当按钮被点击的时候调用的方法
  publicvoid onBtnClick(View v) {
    //code             
   }

2.实现接口 View.OnClickListener。

//自建类实现。(推荐)
 privateclass MyListener implements OnClickListener{ 
      @Override 
       public void onClick(View v) {   
          //TODO code
        }                
  }

button.setOnClickListener(new MyListener());

//匿名内部类。(推荐)
 button= (Button) this.findViewById(R.id.button);

 button.setOnClickListener(newView.OnClickListener() {
  @Override
    publicvoid onClick(View v) {                                
    //code
    }
  });
//activity实现接口,不推荐。
public class MainActivity extends Activity implementsOnClickListener{
 button= (Button) this.findViewById(R.id.button);                   button.setOnClickListener(this)
@Override
   publicvoid onClick(View v) {
    //code
    }
}

EditText

TextView子类,主要用于输入。 文本框可以指定接受的输入类型。

android:inputType="text"//主要值有文本,密码,数字,日期,时间,电话,邮件,web连接。

ImageView ImageButton

ImageView主要用于显示图片,ImageButton多了点击响应,是ImageView的子类。但都继承于View所以他们的区别就像Button和View的一样只是一种变形。
主要属性是图片源和适配模式。

android:src="@drawable/ic_launcher_background"//同样放Drawable对象,但当指定颜色值是,不能让长宽自适应。
android:scaleType="center"/*图片的适配模式
matrix:使用matrix方式进行缩放。
fitXY:横向、纵向独立缩放,以适应该ImageView。
fitStart:保持纵横比缩放图片,并且将图片放在ImageView的左上角。
fitCenter:保持纵横比缩放图片,缩放完成后将图片放在ImageView的中央。
fitEnd:保持纵横比缩放图片,缩放完成后将图片放在ImageView的右下角。
center:把图片放在ImageView的中央,但是不进行任何缩放。
centerCrop:保持纵横比缩放图片,以使图片能完全覆盖ImageView。
centerInside:保持纵横比缩放图片,以使得ImageView能完全显示该图片。*/

RadioButton RadioGroup

RadioButton继承CompoundButton,CompoundButton继承Button,但是在CompoundButton中比普通的按钮多了一个Drawable对象,他就是我们用来勾选所点击的那个小图片。

RadioGroup继承于LinearLayout 主要作用是用来建立一个单选组。必须在RadioGroup中使用RadioButton,否则单纯的RadioButton在点击后是无法复原的。

//XMl drawable问价下的check文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/p1"  android:state_checked="true"/>
    <item android:drawable="@drawable/p2"/>
</selector>

//布局文件
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp">
            <RadioButton android:id="@+id/radiobutton1"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:text="选择1"/>
            <RadioButton android:id="@+id/radiobutton2"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:text="选择2" />
        </RadioGroup>

//代码
   private RadioButton mRadiobutton;
    private TextView mText;

    private RadioButton mRadiobutton1;
    private RadioButton mRadiobutton2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRadiobutton1 = (RadioButton) findViewById(R.id.radiobutton1);
        mRadiobutton1.setButtonDrawable(R.drawable.check);//改变预设的图片。
        mRadiobutton2 = (RadioButton) findViewById(R.id.radiobutton2);
    }

效果:

这里写图片描述
这里写图片描述

确定被选中的按钮。
可以直接获取被选中的RadioButton的id。然后就可以获取到选中的RadioButton。

@IdRes
public int getCheckedRadioButtonId() {
        return mCheckedId;
}

也可以实现接口RadioGroup.OnCheckedChangeListener()来在每次选择改变的时候响应事件。也可以用上面的方法,只记忆最后的选择。具体实现和Button的集中方法一样。

CheckBox

CheckBox数据RadioButton的兄弟类,都继承于CompoundButon,也属于Button的一种,单从视觉效果上看都是用于选择,但是CheckBox没有外部组(RadioGroup),如果单个RadioBUtton和CheckBox比较不同的是RadioButton在选中之后不能取消,需要其他RadioButton来互斥选择。CheckBox是可以单独选择和取消的。其他基本一样,RadioGroup为RadioButton提供了组内互斥的方法。RadioGroup继承于LinearLayout属于布局,属于ViewGroup能管理自己内部的子控件。

//RadioButton的状态改变用的是RadioGroup.OnCheckedChangeListener的实现类
   mRadiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {

            }
        });

//CheckBox状态改变用的是CompoundButton.OnCheckedChangeListener的实现类。
   mCheckbox = (CheckBox) findViewById(R.id.checkbox);
   mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });

ToggleButton Switch

ToggleButton和Switc也都是RadioButton和CheckBox的兄弟类。
ToggleButton可以设置其内按钮点击改变是按钮文字也改变点击改变状态的视觉效果。Drawable样式的改变可以用Background加选择器来改变。主要区别是ToggleButton可以设置两个状态的文字。


    <ToggleButton android:id="@+id/togglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="kai"//checked的文字
        android:textOff="guan"//unchecked的文字
        android:text="222"/>//这个text貌似无效了
        //代码设置 
        mTogglebutton.setTextOn("开");
        mTogglebutton.setTextOff("关");

Switch和ToggleButton很像,他也可以改变状态,当Switch不设置ThumbDrawable(点击图片)把背景图调成和ToggleButton一样,并且ToggleButton不设置开关文字是他们的表现形式是一样的。
Switch内有两个Drawable对象,一个是轨道对象,一个点击的对象。

    <Switch  android:id="@+id/switchbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:track="@drawable/p1"//轨道对象,下层背景的那个。
        android:thumb="@drawable/p2" />//点击对象,上层会动的那个。

MARK(6)

谷神不死是谓玄牝。玄牝之门是谓天地根。绵绵若存,用之不勤。

猜你喜欢

转载自blog.csdn.net/joy_chow/article/details/80736310