これまで、[簡単Androidアプリ]というタイトルで6記事ほど投稿してきましたが今回はこれまでの総集編みたいなものです。
ここまでこのクソみたいなブログを読んでくれたプログラム初心者のあなた!!
あなたに朗報です。
といっても私もまだまだ初心者なのですが…
それはどうでもいいとして、
そう!たった6記事。
開発環境のページを抜けば、たった4記事の内容で簡単なカウンタアプリが作れちゃいます。
もしこれまでjavaやC言語の基礎本しか勉強したことのない人がいれば感動すると思います。私もはじめてGUIアプリケーションを自分で作成した際少し感動してしましました。
では早速コードを見て行きましょう。
Coding
SampleMain.java
メインアクティビティクラス
package com.sample.brogger;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SampleMain extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// レイアウトの指定
setContentView(R.layout.activity_sample_main);
// オブジェクトの作成
final TextView tv = (TextView) findViewById(R.id.pvText);
final Button cnt_bt = (Button) findViewById(R.id.sampleButton);
final Button clear_bt = (Button) findViewById(R.id.clearButton);
// カウントボタンが押された場合のリスナ
cnt_bt.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// pvTextの値を取得
int cnt = Integer.parseInt((String) tv.getText());
// 取得した値 + 1
cnt++;
// ボタンが押された場合TextViewの文字を変更
tv.setText(String.valueOf(cnt));
}
});
// クリアボタンが押された場合のリスナ
clear_bt.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// ボタンが押された場合TextViewの文字を変更
tv.setText("0");
}
});
}
}
activity_sample_main.xml
レイアウトを設定するxml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/gradientbackground"
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="com.sample.brogger.SampleMain" >
<TextView
android:id="@+id/sampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="1031Taku-Programming Note"
android:textSize="70sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/sampleButton"
android:layout_alignLeft="@+id/sampleText"
android:layout_marginBottom="99dp"
android:layout_marginLeft="378dp"
android:text="PV:"
android:textSize="60sp" />
<TextView
android:id="@+id/pvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="23dp"
android:layout_toRightOf="@+id/textView1"
android:text="0"
android:textSize="60sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignTop="@+id/textView3"
android:layout_marginTop="44dp"
android:text="皆さんの力でたくさん上げてあげてください" />
<Button
android:id="@+id/sampleButton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_above="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="62dp"
android:text="1031Taku"
android:textSize="40sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/sampleText"
android:layout_alignParentBottom="true"
android:layout_marginBottom="164dp"
android:text="ボタンをクリックするとPV数が上がります" />
<Button
android:id="@+id/clearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignRight="@+id/sampleButton"
android:layout_marginLeft="112dp"
android:layout_toRightOf="@+id/pvText"
android:text="Clear"
android:textSize="30sp" />
</RelativeLayout>
gradientbackground.xml
背景をグラデーションするxml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#CCFF99"
android:endColor="#CCFFFF"
android:angle="270"/>
<corners android:radius="10dp"/>
</shape>
実行
では実際に実行してみます。
1031Takuボタンを押すと…
解説
今回は総集編なので詳しい解説は以前投稿した記事内に書いてあります。
一応リンクだけ貼っておきますね。
[簡単Androidアプリ] プロジェクトの作成からHello Worldテキストの出力まで
[簡単Androidアプリ] ボタンの利用とリスナーの設定
[簡単Androidアプリ] 背景にグラデーションを与える方法
一応リンクだけ貼っておきますね。
[簡単Androidアプリ] プロジェクトの作成からHello Worldテキストの出力まで
[簡単Androidアプリ] ボタンの利用とリスナーの設定
[簡単Androidアプリ] 背景にグラデーションを与える方法



0 コメント: