Button Control?
So i'm pretty new at this whole development thing for android and I don't know a slick of java. I'm tryin to teach myself this so I thought this would be a great resource. I've read the devoloper.android resources and i still dont understand it so here is my question. I'm making a simple app that will change the background color when the button is pressed. How do i make the button do that?
Any outside resources/examples would be greatly appreciated
Here is my code so far:
IntroActivity.java
package com.flashcalc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class IntroActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/all_white">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@color/all_black"
android:gravity="center_horizontal"/>
<Button android:text="@string/ChangeColor"
android:id="@+id/ChangeColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_horizontal|center"
android:layout_gravity="center_horizontal|center_horizontal|center">
</Button>
</LinearLayout>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Tjs开发者_StackOverflow中文版 Flashlight</string>
<string name="app_name">FlashCalc</string>
<string name="ChangeColor">I Love Buttons</string>
<color name="all_white">#FFFFFF</color>
<color name="all_black">#000000</color>
</resources>
You need to get the view of your linearlayout and set it's background color.
First you need to assign your LinearLayout an ID in your xml. Then in your button's onclicklistener, do this.
LinearLayout ll = (Linearlayout) findViewById(R.id.layoutid);
ll.setBackgroundColor(); //I think this is what it's called
I forget exactly what goes in the setBackgroundColor function, you'll have to look that up.
You should really go through all the hello android tutorials before trying to do stuff on your own like this. Where you haven't even tried anything before coming for help.
There are several things you need to do. First, you need to gave an id to whatever you are going to change the color of, the LinearLayout, TextView or whatever. Then, you'll need to attach code to the button to run when it's clicked. There are two ways to do this. First, you can an onClick handler to your XML:
android:onClick="buttonChangeColor"
And then in your class, add the appropriate method:
public void buttonChangeColor(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.whateverYouCalledThis);
ll.setBackgroundColor(0xffffff); //white
}
You can also attach a method using setOnClickListener: http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)
Another thing that might be helpful is to store your color value in a resource file (res/values/colors.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color_0">#ffffff</color>
</resources>
You can then use that color in your code:
ll.setBackgroundColor(getResources().getColor(R.color.color_0));
精彩评论