Professional Documents
Culture Documents
Lesson
-1-
SampleButton
WinC:PROCLASSLesson1SampleButton / Mac PROCLASSLesson1SampleButton
XML
-2-
Button
strings.xml
-3-
strings.xml
<string name="app_name">SampleButton</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string
<string
<string
<string
name="app_name">SampleButton</string>
name="action_settings">Settings</string>
name="hello_world">Hello world!</string>
name="hello_world">Hello world!</string>
<string
<string
<string
<string
name="app_name">SampleButton</string>
name="action_settings">Settings</string>
name="hello_world">Hello world!</string>
name="button_text"></string>
namebutton_text
-4-
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="@string/button_text" />
MainActivity.java
-5-
-6-
MainActivity
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn;
}
-7-
Button
import
Import
Button
-8-
import
Button
Button
-9-
btn
btn
Button btn;
btnButton
- 10 -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewByIdID
IDR.id.button1ID
R.
- 11 -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.
setOnClickListener
setOnClickListener
setOnClickListener
public class MainActivity extends Activity {
MainActivity
- 12 -
abstract
The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
abstractextendsimplement
abstractmethod
implementmethod
Add unimplemented methodsaddunimplementedmethods
Add unimplemented methodsonClick
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv;
}
TextView
TextViewTextView
Import TextView (android.widget)
- 13 -
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = findViewById(R.id.textView1);
}
R.
findViewById
Add cast to TextView
setText
tv.setText(text);
(text)(OnClick)
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("OnClick");
}
- 14 -
- 15 -
ButtonTextView
XML
- 16 -
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("OnClick");
}
strings.xml
<string
<string
<string
<string
name="app_name">SampleButton</string>
name="action_settings">Settings</string>
name="hello_world">Hello world!</string>
name="button_text"></string>
<string
<string
<string
<string
<string
name="app_name">SampleButton</string>
name="action_settings">Settings</string>
name="hello_world">Hello world!</string>
name="button_text"></string>
name="button_down_text"></string>
UI
IDbutton_down_text
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(R.string.button_down_text);
}
- 17 -
ID
- 18 -
- 19 -
workspaceFortuneTellingText
Application NameFortuneTellingText
ID
begin_text
big_luck
mid_luck
small_luck
button_text
strings.xml
<string
<string
<string
<string
<string
<string
<string
name="app_name">FortuneTellingText</string>
name="action_settings">Settings</string>
name="begin_text"></string>
name="big_luck"></string>
name="mid_luck"></string>
name="small_luck"></string>
name="button_text"></string>
- 20 -
TextView
Text@string/hello_world
@string/begin_text
@string/
textSize
60sp
textSize
Id
@+id/text_view
- 21 -
textButton@string/button_text
- 22 -
Button btn;
Button btn;
Button
Import Button (android.wedget)
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
= findViewById(R.id.button1)
findViewById
Add cast to Button
btn.btn.setO
}
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
setOnClickListener
Let MainActivity implement OnClickListener
- 23 -
onClick
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
TextView tv;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv;
}
TextView
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = findViewById(R.id.text_view);
}
= findViewById(R.id.text_view)
findViewById
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById (R.id.text_view);
}
- 24 -
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById (R.id.text_view);
switch((int)(Math.random() * 3))
{
}
}
TextView tv = (TextView) findViewById (R.id.text_view);
switch((int)(Math.random() * 3))
{
case 0:
tv.setT
}
tv.setT
setText(int resid) : void TextView
}
TextView tv = (TextView) findViewById (R.id.text_view);
switch((int)(Math.random() * 3))
{
case 0:
tv. setText(R.string.big_luck);
}
R.string.big_luck
break;
case 0:
tv. setText(R.string.big_luck);
break;
}
}
- 25 -
case
caseID
ID
- 26 -
- 27 -