6. 2013.05.022013.05.02
Layout StructureLayout Structure
ViewGroupViewGroup
●
A layout or container such asA layout or container such as RelativeLayoutRelativeLayout
oror LinearLayoutLinearLayout
ViewView
●
A child of ViewGroup such asA child of ViewGroup such as buttonbutton oror texttext
fieldfield
12. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
2. Hide the text2. Hide the text
android:visibility="invisible"
Just add the attribution to TextViewJust add the attribution to TextView
But how do I know what
attribution I can use?
13. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
2. Hide the text2. Hide the text
Go toGo to android developer siteandroid developer site
•
ReferenceReference
•
android.wigetandroid.wiget
•
TextViewTextView
•
Inherited XML AttributesInherited XML Attributes
•
Attribute NameAttribute Name
•
android:visibilityandroid:visibility
15. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
3. Make the button show text3. Make the button show text
3-1. add the attribution to button3-1. add the attribution to button
android:onClick="android:onClick="showTextshowText""
3-2. write the code of3-2. write the code of showTextshowText
methodmethod
16. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
3. Make the button show text3. Make the button show text
3-1. add the attribution to button3-1. add the attribution to button
android:onClick="android:onClick="showTextshowText""
3-2. write the code of3-2. write the code of showTextshowText
methodmethod
17. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
3. Make the button show text3. Make the button show text
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_click_text"
android:onClick="showText" />
main.xml
18. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
3. Make the button show text3. Make the button show text
Main.java
public void showText (View view) {
// - 宣告元件
// - 操作元件
}
20. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
3. Make the button show text3. Make the button show text
findViewById example
// 在 Main.java 中宣告 UI 中的 button1
Button button = (Button) findViewById (R.id.button1);
// 在 Main.java 中宣告 UI 中的 TextView
TextView textView = (TextView) findViewById (R.id.tv1);
Main.java
21. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
3. Make the button show text3. Make the button show text
R.id.tv1 ?什麼東西?好像沒看過?
TextView 預設沒給定操作 id ,加上它吧!
main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:id="@+id/tv1"
android:text="@string/hello_world"
android:visibility="invisible" />
22. 2013.05.022013.05.02
Add behavior (cont.)Add behavior (cont.)
3. Make the button show text3. Make the button show text
Main.java
public void showText(View view) {
Button button = (Button) findViewById(R.id.button1);
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setVisibility(View.VISIBLE);
}