基本控件使用(一)

 

 

(一)控件的方式

1.用XML代码定义

<TextView

                android:layout_width="fill_parent"

                android:layout_height="wrap_content

                android:text="Hello World"

                android:textSize="40sp"

/>

2.用Java代码定义

TextView tv=new TextView(this);//new一个标签控件

tv.setText("Hello World");//设置显示的文本

setContentView(tv);//将该控件设置为窗口的显示内容

(二)控件的设置

1.设置控件的ID值

格式:@+id/ID值,实例:android:id="@+id/tv"

2.查找文件中的ID值实例化控件

TextView text=(TextView)super.findViewByID(R.id.mytext);

3.获取控件中的字符串

String text=tv.getText().toString();

4.设置控件中字符串

text.setText("今天天气真好");

原文地址:https://www.cnblogs.com/fxx0129-nn/p/6910794.html