class MainActivity
package org.androidtown.myselector;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "버튼 클릭 -1" , Toast.LENGTH_SHORT).show();
}
});
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="org.androidtown.myselector.MainActivity">
<!--백그라운드에 xml 을 지정한다.-->
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/button"
android:background="@drawable/arrow_left_selector"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/button1"
android:layout_alignStart="@+id/button1"
android:layout_marginTop="29dp" />
<org.androidtown.myselector.BitmapButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/button1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="69dp" />
</RelativeLayout>
class BitmapButton
package org.androidtown.myselector;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
/**
* Created by choi on 2017-04-08.
*/
public class BitmapButton extends Button{
public BitmapButton(Context context) {
super(context);
init();
}
public BitmapButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
setBackgroundResource(R.drawable.arrow_left_normal);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action =event.getAction();
if(action==MotionEvent.ACTION_DOWN){
setBackgroundResource(R.drawable.arrow_left_clicked);
}else if(action==MotionEvent.ACTION_UP){
setBackgroundResource(R.drawable.arrow_left_normal);
}
return true;
}
}
arrow_left_selector.xml
<?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/arrow_left_clicked"
/>
<!--정상적인경우-->
<item
android:drawable="@drawable/arrow_left_normal"
/>
</selector>
댓글 ( 4)
댓글 남기기