You are on page 1of 27

android

Lesson

-1-

Copyright 2014 PROCLASS All Rights Reserved.


SampleButton
WinC:PROCLASSLesson1SampleButton / Mac PROCLASSLesson1SampleButton

XML

-2-

Copyright 2014 PROCLASS All Rights Reserved.


Button

strings.xml

-3-

Copyright 2014 PROCLASS All Rights Reserved.

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-

Copyright 2014 PROCLASS All Rights Reserved.


<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-

Copyright 2014 PROCLASS All Rights Reserved.

-6-

Copyright 2014 PROCLASS All Rights Reserved.


MainActivity

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn;
}

-7-

Copyright 2014 PROCLASS All Rights Reserved.

Button

import
Import

Button

-8-

Copyright 2014 PROCLASS All Rights Reserved.

import

Button

Button

-9-

Copyright 2014 PROCLASS All Rights Reserved.


btn

btn

Button btn;

btnButton

- 10 -

Copyright 2014 PROCLASS All Rights Reserved.


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.button1);


}

findViewByIdID
IDR.id.button1ID

R.

- 11 -

Copyright 2014 PROCLASS All Rights Reserved.


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.button1);


btn.setOnClickListener(this);

btn.
setOnClickListener

setOnClickListener

Let MainActivity implement OnClickListener

setOnClickListener
public class MainActivity extends Activity {

public class MainActivity extends Activity implements OnClickListener {

MainActivity
- 12 -

Copyright 2014 PROCLASS All Rights Reserved.


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 -

Copyright 2014 PROCLASS All Rights Reserved.


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = findViewById(R.id.textView1);
}

R.
findViewById
Add cast to TextView

TextView tv = (TextView) findViewById(R.id.textView1);


tv.
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
tv.
}

setText

setText(CharSequence text) : void TextView

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 -

Copyright 2014 PROCLASS All Rights Reserved.

- 15 -

Copyright 2014 PROCLASS All Rights Reserved.

ButtonTextView

XML

- 16 -

Copyright 2014 PROCLASS All Rights Reserved.


@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

Copyright 2014 PROCLASS All Rights Reserved.

- 18 -

Copyright 2014 PROCLASS All Rights Reserved.

- 19 -

Copyright 2014 PROCLASS All Rights Reserved.


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 -

Copyright 2014 PROCLASS All Rights Reserved.


TextView

Text@string/hello_world
@string/begin_text
@string/

textSize
60sp

textSize

Id

@+id/text_view

- 21 -

Copyright 2014 PROCLASS All Rights Reserved.

textButton@string/button_text

- 22 -

Copyright 2014 PROCLASS All Rights Reserved.

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

Button btn = findViewById(R.id.button1);


}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.button1);


btn.

btn.btn.setO

setOnClickListener(OnClickListener l) : void - View


this

}
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}

setOnClickListener
Let MainActivity implement OnClickListener

- 23 -

Copyright 2014 PROCLASS All Rights Reserved.

MainActivityAdd unimplemented methods


public class MainActivity extends Activity implements OnClickListener {

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

Import TextView (android.widget)

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = findViewById(R.id.text_view);
}

= findViewById(R.id.text_view)
findViewById

Add cast to TextView

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById (R.id.text_view);
}

- 24 -

Copyright 2014 PROCLASS All Rights Reserved.

@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 -

Copyright 2014 PROCLASS All Rights Reserved.

case 1case 2case 0casebreak


TextView tv = (TextView) findViewById (R.id.text_view);
switch((int)(Math.random() * 3))
{
case 0:
tv. setText(R.string.big_luck);
break;
case 0:
tv. setText(R.string.big_luck);
break;
case 0:
tv. setText(R.string.big_luck);
break;
}

case

TextView tv = (TextView) findViewById (R.id.text_view);


switch((int)(Math.random() * 3))
{
case 0:
tv. setText(R.string.big_luck);
break;
case 1:
tv. setText(R.string.mid_luck);
break;
case 2:
tv. setText(R.string.small_luck);
break;
}

caseID

ID

- 26 -

Copyright 2014 PROCLASS All Rights Reserved.

- 27 -

Copyright 2014 PROCLASS All Rights Reserved.

You might also like