TextView文本框实验
1. 实验内容简介
(1)TextView定义了文本框操作的基本方法,它是一个不可编辑的文本框,往往用来在屏幕中显示静态字符串。
通过以下方法对文本框进行设置,如下:
setText("Look,I have been changed!");//设置文本内容
setTextColor(Color.rgb(0,255,0));//设置文本颜色
setTextSize(20);//设置文本大小,以像素为单位
(2)文本框使用步骤:
步骤1:声明变量
步骤2:通过FindViewById关联或绑定
步骤3:监听用户输入动作
(3)常用快捷键的使用方法
ctrl+shift+f 自动排版对齐
ctrl+shift+o 自动导入库
alt+/ 自动补齐代码
2. UI界面布局
对应的大纲:
对应的属性框:
3. 代码编写和调试
(1)MainActivity.java
public class MainActivity extends Activity {
TextView mtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtv=(TextView)this.findViewById(R.id.textView1);
mtv.setText("Look,I have been changed!");
mtv.setTextColor(Color.rgb(0,255,0));
mtv.setTextSize(20);
}
}
(2)activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="This is a demo"
android:textColor="#561b44"
android:textSize="20sp" />
</RelativeLayout>
说明:RelativeLayout标签包含一个TextView的子标签,这个标签定义了一个TextView的对象。程序会根据这个标签的定义加载一个文本框。android:text属性表示TextView组件所显示的内容