案例一
- 事件监听(三种写法)
1.1 标签上直接绑定监听方法
public void xxx(View view)
//第一种
public void xxx(View view)
1.2 创建监听器对象,组件再绑定监听器对象
1.2.1 匿名内部类
1.2.2 使用匿名内部类并定义成全局的属性
第二种:
Button viewById = findViewById(R.Id.);
viewById.setOnClickListtener(new View.OnClickListtener());
public void onClick(View view){
}
- 文本框(TextView)
2.1 简介
TextView直接继承View,作用就是在界面上显示文本(类似于Swing中的JLabel),同时它还是EditText、Button两个UI组件类的父类。
另外Android关闭了它的文字编辑功能,如果想编辑内容,则可以使用EditText。
2.2 TextView常用属性
2.2.1 text
2.2.2 autoLink
2.2.3 singleLine(已过时,现在应为:android:lines)
2.2.4 lines
2.2.5 minLines
2.2.6 textColor
2.2.7 textSize
2.2.8 textStyle
字体风格粗体、斜体
2.2.9 backgroud
设置背景,可以是颜色或图片
2.2.10 drawableXX
设置文本的icon
2.2.11 ellipsize 当文字长度超过textview宽度时的显示方式
android:ellipsize=“start” 省略号显示在开头 “…pedia”
android:ellipsize=“end” 省略号显示在结尾 “encyc…”
android:ellipsize=“middle” 省略号显示在中间 “en…dia”
android:ellipsize=“marquee” 以横向滚动方式显示(另外还需要设置其它三个属性,否则不滚动,即必须让该控件获得焦点)
案例二 常用编辑框
3. 编辑框(EditText)
3.1 简介
EditText和TextView非常相似,它与TextView共用了绝大总分XML属性和文法,
二者最大区别在于:EditText可以接受用户输入。
3.2 常用属性
3.2.1 inputType
它是EditText组件最重要的属性,它相当于HTML中标签的type属性,用于EditText指定输入组件的类型。
常用取值有:number|numberPassword|date|phone
3.2.2 hint:提示字符信息
计算器原图:
//计算器的按钮布局xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:rowCount="6"
android:columnCount="4"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_main_text"
android:layout_width="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_columnSpan="4"
android:layout_height="wrap_content"
android:hint="请输入数字"
android:onClick="rest"
android:text=""
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="3"/>
<Button
android:id="@+id/bt_chu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="/"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="6"/>
<Button
android:id="@+id/bt_chen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="*"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="9"/>
<Button
android:id="@+id/bt_jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="-"/>
<Button
android:layout_width="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_columnSpan="2"
android:layout_height="wrap_content"
android:onClick="add"
android:text="0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="."/>
<Button
android:id="@+id/bt_add"
android:layout_width="wrap_content"
android:layout_rowSpan="2"
android:layout_gravity="fill_vertical"
android:layout_height="wrap_content"
android:onClick="add"
android:text="+"/>
<Button
android:id="@+id/bt_sum"
android:layout_width="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_columnSpan="3"
android:layout_height="wrap_content"
android:onClick="add"
android:text="="/>
<Space />
</GridLayout>
计算器实现代码
public void add(View view) {
Button btn = (Button) view;获得所有的按钮
TextView viewById = findViewById(R.id.et_main_text);//获得输入框
CharSequence text = btn.getText();/获得输入框的值
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();//弹框
if (!"=".equals(text)) {
viewById.setText(viewById.getText() + "" + text);//把点击按钮的值放到输入框里
} else {
String str = "";
if ((viewById.getText()+"").contains("+")) {
str = "+";
}if ((viewById.getText()+"").contains("-")) {
str = "-";
}if ((viewById.getText()+"").contains("*")) {
str = "*";
}if ((viewById.getText()+"").contains("/")) {
str = "/";
}
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
String text1 = viewById.getText() + "";
String num1 = text1.substring(0, text1.indexOf(str));
String num2 = text1.substring(text1.indexOf(str) + 1);
viewById.setText(calcs(str, num1, num2) + "");
}
}
private int calcs(String str, String num1, String num2) {
Toast.makeText(this, "34455", Toast.LENGTH_SHORT).show();
int sum=0;
switch (str) {
case "+":
sum = Integer.parseInt(num1) + Integer.parseInt(num2);
break;
case "-":
sum = Integer.parseInt(num1) - Integer.parseInt(num2);
break;
case "*":
sum = Integer.parseInt(num1) * Integer.parseInt(num2);
break;
case "/":
sum = Integer.parseInt(num1) / Integer.parseInt(num2);
break;
}
return sum;
}
案例三:椭圆形、背景渐变的文本框
4. Android资源
4.1 什么是资源
指在res文件夹下的xml文件,每一种类型的xml文件都对应了一种资源。
例如:外形资源(shape)、尺寸资源(dimension)、颜色资源(color)等
4.2 尺寸资源和颜色资源放res/values目录下
4.3 *.xml(文本框的外形资源)可放到drawable目录下
4.4 设置渐变色与背景色只能取其一,后面会覆盖前面的
案例四:根据获得/失去焦点边框变色编辑框
5. 选择器
5.1 作用:根据控件状态显示不同样式
5.2 Item: 指定不同状态下控件显示哪个样式
核心点:
1、点击事件的添加
2、输入框引入外形资源调节样式