基本控件使用(一)
阿新 • • 發佈:2017-05-27
url ext .html his post 方式 blog tex ont
(一)控件的方式
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("今天天氣真好");
基本控件使用(一)