显示和输入文本信息
- TextView 显示文本框控件
- android:id 控件的id
- android:layout_width 控件宽度
- fill_parent 当前控件铺满父类容器,2.3api之前添加的属性值
- match_parent 当前控件铺满父类容器,2.3api之后添加的属性值
- wrap_content 包裹实际文本内容
- android:layout_height 控件高度
- android:text 文本内容
- android:textSize 文本大小
- android:textColor 文本颜色
- android:background 控件背景
EditText 输入文本框
- 具有以上所有属性
- android:hint 输入提示文本
- android:inputType 输入文本类型
将布局xml文件引入到activity中
setContentView(R.layout.activity_main);
显示图片
- ImageView
- android:src=”@drawable/ic_launcher” ImageView的内容图像
- android:background=”@drawable/ic_launcher” ImageView的背景图片
- android:background=”#111111” 也可以是颜色
按钮
- Button
- ImageButton 图片按钮
- 共同点
- 都有明显的点击效果
- 都有background
- 区别
- Button有text属性,ImageButton没有
- ImageButton有src属性,Button没有
监听
- onClick事件
- 任何控件都有一个onClick事件
三种实现方法
匿名内部类
/** * 1.初始化当前所需要的控件 */ loginButton = (Button) findViewById(R.id.button); //返回一个view /** * 2.设置button监听器 */ loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("click"); } });
独立类
写一个类实现OnClickListner即可- 实现接口
让MainActivity实现OnClickListner接口,实现它的OnClick方法,然后在调用loginButton.setOnClickListener(this)
即可
- onClick事件
输出提示信息
- log.i() 输出到控制台
- toast.maketext() 相当于js的alert,不过现实更加友好
TextView实例(实现文字滚动)
1.添加三个属性
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
2.自定义类
package com.example.sujinming.testandroid;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by sujinming on 2016/9/9.
*/
public class MarqueeText extends TextView {
public MarqueeText(Context context) {
super(context);
}
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
return true;
}
}
3.使用自定义的类声明控件
<com.example.sujinming.testandroid.MarqueeText/>
AutoCompleteTextView
- 属性
- android:completionThreshold = “2” 设置输入多少字符自动匹配,默认值是2
- 用法
/**
* 1.初始化控件
*/
acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
/**
* 2. 需要一个适配器
* 3. 初始化数据源--去匹配文本框中文本框输入的内容
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,res);
/**
* 4. 将adapter与当前acTv绑定
*/
acTextView.setAdapter(adapter);
- MultiAutoCompleteTextView 支持多选择
macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
设置分隔符,此句为以逗号为分隔符
ToggleButton
- ToggleButton有两种状态,选中和未选中状态显示不同的文本
- 属性
- android:checked=”ture”
- android:textOff=”关”
- android:textOn=”开”
- 实例(开关效果)
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
img.setBackgroundResource(isChecked?R.mipmap.ic_launcher:R.drawable.offf);
}
});
CheckBox复选框
- 两种状态
选中和未选中 - 属性
- android:checked=”false”
setOnCheckedChangeListener
作为选中和未选中的监听- 使用style进行样式的设计
RadioGroup和RadioButtton
- RadioGroup属性
- android:orientation=”vertical” 当中RadioButton的排布方式
setOnCheckedChangeListener
作为选中和未选中的监听