サンプル集  >  Android  >  ボタンA
ボタンA
2013/11/29

Androidアプリで、ボタンを押したらテキストに「OnClick!!」と表示するアプリを作成します。

◆環境
OS Windows 7 Professional Service Pack 1
eclipse 4.2.2
Android 4.4
Android端末 Nexus7

@eclipseで[File]-[New]-[Project]を選択します。

ANew Projectウィンドウが開いたら[Android]-[Android Application Project]を選択し「Next」ボタンを押します。

BNew Android Applicationウィンドウが開いたら「Aplication Name」に AndroidClient2 と入力し「Next」ボタンを押します。

CConfigure Projectウィンドウが開いたらそのまま「Next」ボタンを押します。

DConfigure the attributes of the icon setウィンドウが開いたらそのまま「Next」ボタンを押します。

ECreate Activityウィンドウが開いたらそのまま「Next」ボタンを押します。

FBlank Activityウィンドウが開いたらそのまま「Finish」ボタンを押します。

編集

@Package Explorer で activity_main.xml をダブルクリックして開きます。

AForm Widgets の Button をドラッグして画面の適当な位置にドロップします。

Bactivity_main.xmlタブを選んでXML編集モードにします。

CButton の定義に android:onClick="button1_click" を追記します。

activity_main.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: 
<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: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" >

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:onClick="button1_click"
        android:text="Button" />

</RelativeLayout>

DMainActivity.java をダブルクリックで開きます。

button1_click関数を追加し、txt に「OnClick!!」と設定するようプログラムを書きました。

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: 
31: 
32: 
package com.example.androidclient2;

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

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void button1_click( View v )
    {
        // TextViewを取得
        TextView txt = ( TextView )findViewById( R.id.textView1 );

        txt.setText( "OnClick!!" );
    }
}

実行したところ、ボタンを押すとテキストに「OnClick!!」と表示されました。

ボタン で作成したプログラムでは setOnClickListener() でボタンを押したときの処理を追加しましたが、 こちらの方がプログラムが見やすくて簡単でいいですね。

▲ PageTop  ■ Home


Copyright (C) 2013 ymlib.com