2.
Android
: Android Studio.
. . . , . , .
. "Hello, World" () . . Toggle Fill Width, ↔. - Width match_parent. android:layout_width.
Outline button1. Properties. . , Id @+id/buttonRed @+id/button1, - @+id/buttonYellow.
, . Graphical Layout activity_main.xml. XML- , .
</RelativeLayout>. android:layout_below):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world" />
<Button
android:id="@+id/buttonRed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="@+id/buttonYellow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
|
|
android:layout_below="@+id/buttonRed"
android:text="Button" />
<Button
android:id="@+id/buttonGreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/buttonYellow"
android:text="Button" />
</RelativeLayout>
, . , . Ctrl+Space, . , Enter, .
Button , Ƹ . 1 Text . . , . , , ( ). , .
. Graphical Layout buttonRed. Text. . ٸ . Resource Chooser.
New String... . Create New Android String (. ) String, New R.string , , red. (Ƹ Ǹ).
ٸ OK, . , , Text @string/red, @string/yellow, @string/green.
, strings.xml, res/values . .
<resources>
<string name="app_name">TrafficLight</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main"></string>
<string name="red"></string>
<string name="yellow">Ƹ</string>
<string name="green"></string>
</resources>
TextView. , , , . , Ƹ , , , . . Text TextView . New String..., red, , , OK ( ).
|
|
, TextView.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/red"
tools:context=".MainActivity" />
, . . .
. . strings.xml, Resources. Add..., Color OK. Color. Name* Value* redColor #FFFF0000, . yellowColor #FFFF00, - greenColor #FF00FF00.
, . colors.xml.
, RelativeLayout. Background @color/redColor.
android:background="@color/redColor" RelativeLayout.
. , , , RelativeLayout.
, , , .
, TextView. й1 onClick. . onClick Button:
android:onClick="buttonRed_Click"
MainActivity.java. onCreate():
// onCreate()
public TextView tvInfo;
public RelativeLayout relativeLayout;
// onCreate()
relativeLayout = (RelativeLayout)findViewById(R.id.relativelayout);
tvInfo = (TextView)findViewById(R.id.textView1);
onCreate():
public void buttonRed_Click(View v)
{
tvInfo.setText(R.string.red);
relativeLayout.setBackgroundResource(R.color.redColor);
}
R , . RelativeLayout TextView, activity_main.xml android:id="@+id/relativelayout" android:id="@+id/textView1". , .
() . butGreen On click buttonGreen_Click. android:onClick="buttonGreen_Click" . Click , .
|
|
- OnClickListener.
Button buttonYellow = (Button)findViewById(R.id.buttonYellow);
buttonYellow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tvInfo.setText(R.string.yellow);
relativeLayout.setBackgroundResource(R.color.yellowColor);
}
});
- .
:
package ru.alexanderklimov.trafficlight;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
public TextView tvInfo;
public RelativeLayout relativeLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout)findViewById(R.id.relativelayout);
tvInfo = (TextView)findViewById(R.id.textView1);
Button buttonYellow = (Button)findViewById(R.id.buttonYellow);
buttonYellow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tvInfo.setText(R.string.yellow);
relativeLayout.setBackgroundResource(R.color.yellowColor);
}
});
}
public void buttonRed_Click(View v)
{
tvInfo.setText(R.string.red);
relativeLayout.setBackgroundResource(R.color.redColor);
}
public void buttonGreen_Click(View v)
{
tvInfo.setText(R.string.green);
relativeLayout.setBackgroundResource(R.color.greenColor);
}
}