본문 바로가기
카테고리 없음

버튼을 press 할때 배경색 바꾸기

by ByteBridge 2013. 2. 20.
반응형

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color_red">#FF0000</color>
    
</resources>


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <drawable name="red_bg">#FF0000</drawable>
    <drawable name="green_bg">#00FF00</drawable>
    
</resources>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"
        android:drawable="@drawable/red_bg"/>
    
    <item android:drawable="@drawable/green_bg"></item>
    

</selector>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="33dp"
        android:background="@drawable/button_bg"
        android:text="@string/button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/rotate_image" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="message" />

</LinearLayout>


package com.wooriworld.Assign_Cal;

import android.R.integer;
import android.R.interpolator;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {

RotateDrawable mDrawable;//로테이트 드로블 정의 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        
        //이미지 로테이트 예제 
        ImageView imageView = (ImageView)findViewById(R.id.imageView1);
        mDrawable = (RotateDrawable)imageView.getDrawable();
        Button btn13=(Button)findViewById(R.id.button13);
        btn13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int level = mDrawable.getLevel();
level=(level+1000)%10000;
mDrawable.setLevel(level);
}
} );
        //칼라값과 스트링 가져오기 
        //칼라값은 int 로 받음 
        int myColor = getResources().getColor(R.color.color_red);
         String mymessage = getResources().getString(R.string.my_message);
         String[] myArray = getResources().getStringArray(R.array.myArray);
        
         Drawable mypicture = getResources().getDrawable(R.drawable.love);
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //
    
}




반응형