Hatena::Groupandroid

組み込みプログラマのAndroid開発覚書

[Ruby] [Android入門] [Android 開発入門] [Android 開発チュートリアル] [逆引きAndroid] [Ruby] [Xperia] [チームらくらく]
 | 

2013-08-20

Buttonを使う

16:12

Android studio使用

src/main/java/(中略)/MainActivity

package com.example.button;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.button.R.id;


public class MainActivity extends Activity {

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


        Button button = (Button) findViewById(id.button);
            button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button button = (Button) v;
                Toast.makeText(MainActivity.this, "onClick()",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

src/main/res/layout/activity_main.xml

<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/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button" />

</RelativeLayout>

checkBoxを使う

18:14

Android studio使用

src/main/java/(中略)/MainActivity

package com.example.checkboxsample1;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

import com.example.checkboxsample1.R.id;


public class MainActivity extends Activity {

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

        CheckBox checkBox = (CheckBox) findViewById(id.checkbox);
        checkBox.setChecked(true);

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                boolean checked = checkBox.isChecked();
                Toast.makeText(MainActivity.this,"onClick():" + String.valueOf(checked),Toast.LENGTH_SHORT).show();
            }
        });


    }

}


src/main/res/layout/activity_main.xml

<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">

    <CheckBox android:id="@+id/checkbox"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="CheckBox" />
</RelativeLayout>
 |