コンテンツにスキップ

Button

MainActivity.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.example.sasakiakira.uisample2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /** Button. */
    private Button mButton01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton01 = (Button) findViewById(R.id.button01);
        mButton01.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if  (v.equals(mButton01)) {
            Toast.makeText(this, "TEST", Toast.LENGTH_LONG).show();
        }
    }
}

activity_main.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?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:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:id="@+id/button01"
        android:text="buttonSample1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </Button>

</RelativeLayout>

レイアウトを改良する

res/drawable/button_anim.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:state_pressed="true">
        <shape android:shape="rectangle" android:useLevel="false">
            <solid android:color="@android:color/darker_gray" />
            <stroke
                android:color="#CCCCCC"
                android:width="1dp" />

            <corners
                android:radius="10dp" />
        </shape>
    </item>


    <item android:state_pressed="false">
        <shape android:shape="rectangle" android:useLevel="false">
            <solid android:color="#00a0e9" />
            <stroke
                android:color="#CCCCCC"
                android:width="1dp" />

            <corners
                android:radius="10dp" />
        </shape>
    </item>

</selector>

activity_main.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?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:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:id="@+id/button01"
        android:text="buttonSample1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_anim"
        >
    </Button>

</RelativeLayout>