- TextView 文本框
- EditText控件
- Button 与 ImageButton
- ImageView
- RadioButton
- CheckBox复选框
TextView 文本框
,用于显示文本的控件
1) 代码
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
2)TextView的常用属性:
-id: 为TextView设置一个组件id,根据id,我们可以在Java代码中获取到该对象,然后进行相关属性的设置
-layout_width: 组件宽度,一般有3种属性 wrap_content ,match_parent 和自己输入宽度
wrap_content表示根据组件大小确定宽度即组件包含文字所占宽度越多,组件越宽;
match_parent表示填满该组件的父容器即包含了该组件的组件如代码中的LinearLayout ;
-layout_hight: 组件高度,一般也有3种属性,同上;
-gravity: 设置控件中内容的对齐方向,即文字内容的对齐方向;
-text: 设置显示的文本内容(建议把字符串写到values/string.xml文件中,然后使用@String/xxx的方式获取 , 也可以直接写在""中);
-textColor: 设置文本的颜色(后面会附上常用颜色码);
-textStyle: 设置文本显示风格,三个可选属性 normal(无效果) ,bold(加粗) ,italic(斜体);
-background: 控件背景颜色,即填充整个控件的颜色;
3)在textview控件中引入string.xml中写好的文本的方法:
第一步:现在string.xml文件中加入你要显示的文本,并命名:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyActivity</string>
<string name="hello">hello ,你好</string>
</resources>
第二步:然后在控件中引入:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
EditText控件
,与TextView控件类似,最大的区别就是EdiText允许用户输入
1) 代码
<EditText
android:
/>
2)EditText常用属性:
-id: 为EditText设置一个组件id;
-layout_weith: 设置控件的宽度;
-layout_hight: 设置控件的高度;
-inputType(textPassword): 设置输入内容为密码;
-inputType(Phone): 设置输入的内容为号码;
-maxLength: 设置输入的最大文本数, 参数为数字例如 android:maxLength=“5”;
-maxLines: 设置最大行数,参数为数字;
-minLines: 设置最小行数,参数为数字;
-hint: 设置默认提示文本;
-textColorHint: 设置默认提示文本颜色;
-capitalize: 设置英文字母大小写属性,参数包含:
sentences: 仅第一个字母大写
words: 单词首字母大写
characyers: 全部字母大写;
3) EditText控件的绑定:
绑定控件的方法也都一样,在onCreate方法中使用findViewById()方法:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定main.xml文件中的EdiText控件
EditText editText = (EditText) findViewById(R.id.edit);
}
}
4) EditText获取用户输入:
绑定后获取用户的输入很容易,使用getText()方法即可:
editText.getText();//获取用户输入的方法
值得一提的是这个方法直接写在绑定好的EditText语句后面是不行的!准确的说是获取不到输入的内容,我们需要一个 触发事件在输入完成后再触发获取输入的方法,就能正常拿到数据啦!下面看代码:
EditText editText;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit);
text = (TextView) findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText(editText.getText());;
}
});
}
text.setOnClickListener()方法是text监听方法.用来监听TextView的点击事件。
整个流程就是在输入文本后点击text然后text的文本就会被替换成你输入的文本.
Button 与 ImageButton
本节学习Android基本控件按钮控件,Button和ImageButton用法基本类似,所以本节重点讲解Button控件。
1)代码:
<Button
android: , Toast.LENGTH_SHORT).show();
}
});
}
}
ImageView
,ImageView 图像视图,顾名思义,是一个用来显示图片的控件。
1)代码:
<ImageView
android:/>
2)ImageView的常用属性:
-background 设置控件背景 当固定宽度时会拉伸图片
-src 设置控件填充内容 固定宽度时不会拉伸图片
3)绑定
img = (ImageView) findViewById(R.id.imageview);
ImageView没有啥监听方法,一般ImageView用来实现app的图片轮播功能。这会用到修改图片显示的方法
常用的修改方法:
-setImageResource(int id); 使用内部资源图片替换默认图片,id为R.drawable.图片名称
-setImageBitmap(Bitmap bitmap); 使用bitmap 替换默认图片,bitmap一般通过网络获取
使用方法:
img.setImageResource(R.mipmap.ic_launcher);
RadioButton
,RadioButton单选按钮,就是几个选项只能选中一个。 因此我们要把RadioButton放到RadioGroup按钮组中,从而实现 单选功能!
1)代码:
<RadioGroup
android: + radbtn.getText(), Toast.LENGTH_LONG).show();
}
});
获得RadioButton相关信息的方法:
-getClidCont() 获得按钮组中的单选按钮的数目
-getClindAt() 根据索引值获得单选按钮 (参数为索引值 0,1,2,3..)
-isChecked() 判断按钮是否被选中(参数为true/false)
CheckBox复选框
1)代码:
<CheckBox
android:;
Toast.makeText(this,choose,Toast.LENGTH_SHORT).show();
}
}