Activity_Main.xml:
<RelativeLayoutxmlns: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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/l1"
android:background="@color/black"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<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:layout_marginLeft="44dp"
android:layout_marginTop="92dp"
android:text="@string/b1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="62dp"
android:layout_toRightOf="@+id/button1"
android:text="@string/b2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="40dp"
android:text="@string/b3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:text="@string/b4" />
</RelativeLayout>
MainActivity.java
package
com.example.prac14;
importandroid.os.Bundle;
importandroid.annotation.SuppressLint;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
@SuppressLint("ResourceAsColor")
publicclassMainActivityextends
Activity implementsOnClickListener {
Button
b1,b2,b3,b4;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main,
menu);
returntrue;
}
@SuppressLint("ResourceAsColor")
@Override
publicvoidonClick(View
v) {
TextView
txt = (TextView )findViewById(R.id.textView1);
switch(v.getId())
{
case
R.id.button1:
txt.setBackgroundColor(getResources().getColor(R.color.red));
break;
case
R.id.button2:
txt.setBackgroundColor(getResources().getColor(R.color.yellow));
break;
case
R.id.button3:
txt.setBackgroundColor(getResources().getColor(R.color.green));
break;
case
R.id.button4:
txt.setBackgroundColor(getResources().getColor(R.color.orange));
break;
default:
}
}
}
0 Comments