夜の歌

プログラミング、音楽、本について緩く書いていきます。

水曜日の日記(9月25日)

仕事3日目。コード読む集中力がなかなか戻らない。。意識が飛びそうになってたけど、なんとか持ちこたえる。でも、前日よりはましな感じ。

MainActivity.Java

package com.example.test3;

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

public class MainActivity extends Activity implements View.OnClickListener{

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

        Button button = (Button) findViewById(R.id.test);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Toast.makeText(this,"test",Toast.LENGTH_SHORT).show();
    }
}

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50px"
        android:text="@string/hello_world"/>

</RelativeLayout>

ボタンを押してトーストを表示する簡単なコード。なるほど、慣れていこう。
そんな感じで。