网友通过本文主要向大家介绍了android 表单,android 表单提交,android 表单上传图片,android 表单上传,android 表单布局等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android表单UI及相应控件的事件处理,android表单ui控件
一、
Toast

Toast是一种轻量级的提示工具,可显示一行文本对用户的操作进行提示响应
用法:
Toast.makeText(context,text,time).show();
context:上下文 、text:显示文本内容、time:显示时长
Toast.LENGTH_SHORT(短时间显示)
Toast.LENGTH_LONG(长时间显示)
例:
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ceshi.this, "你点击了按钮", Toast.LENGTH_SHORT).show();
}
});
二、
TextView——文本显示控件
常用属性
text 显示的文本内容 textColor 文本颜色
textSize 字体大小 singleLine 单行显示true|false
gravity 内容的重心位置 drawableLeft 文字左边显示的图片
drawableRight 文字右边显示的图片
drawableTop 文字上边显示的图片
drawableBottom 文字下边显示的图片
例:
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#00ff00"
android:id="@+id/textView2"
android:layout_alignBottom="@id/f1"
android:layout_centerInParent="true"
android:text="来一碗鱼丸粗面"
android:textColor="#f00"
android:textSize="20sp"
android:gravity="bottom|center"/>
常用方法
nsetText() 设置显示文字
nsetTextColor() 设置文字颜色,参数为颜色值
nsetTextSize() 设置文字尺寸,参数1为尺寸单位,2为尺寸大小
ngetText() 获取显示的文字
三、
Button——按钮
常用属性
TextView的属性
还有一个onClick,但是这个一般用不到,
Button的方法见上面的Toast的用法
四、
EditText——文本输入控件,继承自TextView
常用属性
inputType 限制能够输入文本类型
hint 显示的提示 值为 string
maxLength 能够输入的最大长度 值为 int
focusable 能否获取焦点 值为 boolean
lines 显示的行数 值为 int
maxLines 最大显示行数 值为 int
enable 是否可用 值为 boolean
例:
<EditText
android:id="@+id/edittext_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码!"
android:textSize="16sp"
android:textColor="#f00"
android:textColorHighlight="#a00000ff"
android:drawableLeft="@mipmap/ic_launcher"
android:inputType="textPassword" />
常用方法
setHint() 设置提示文本
setInputType() 设置输入限制类型
setFocusable() 设置能否获取焦点
setEnable() 设置是否可用
isEnable() 是否可用
具体代码:
edittext1.getText().toString();//获取EditView的文本值
edittext1.addTextChangedListener(watcher);//文本监听
private TextWatcher watcher = new TextWatcher() {
/*文本改变之前*/
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/*当文本改变的时候*/
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
/*文本改变之后*/
@Override
public void afterTextChanged(Editable s) {
//获取已经输入的的文字总数
int currentCount = s.length();
//计算还能输入多少个字符,记住,数字不能直接写
textview1.setText(String.valueOf(max - currentCount));
}
};
五、CheckBox——复选框,继承自TextView

常用属性
TextView的属性
button 复选指示按钮
checked 是否选中
例:
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox1"
android:text="敲代码" />
常用方法
TextView的方法
setChecked() 设置是否选中
isChecked() 是否选中的
setOnCheckedChangeListener() 设置选中改变监听
具体代码
checkboxall.setOnCheckedChangeListener(changeListener);
checkboxall.setChecked(false);
<!--布局-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textviewtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="平时喜欢做什么事情?" />
<CheckBox
android:id="@+id/checkboxall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textviewtitle"
android:layout_alignTop="@id/textviewtitle"
android:layout_toRightOf="@id/textviewtitle"
android:text="全选" />
<!--内容的CheckBox-->
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"