2013年2月26日火曜日

Androidアプリ開発 ボタンとToast

Android開発のサンプルです。ボタンを押すと「ボタンが押されました。」というメッセージをしばらくの間、表示します。

githubソースコード
<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toast"
android:layout_gravity="center">
</Button>
</RelativeLayout>
package com.example.andtoast;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button button0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0=(Button)findViewById(R.id.button0);
button0.setOnClickListener(this);
}
public void onClick(View v){
Toast.makeText(this, "ボタンが押されました。", Toast.LENGTH_LONG).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndToast</string>
<string name="hello_world">Hello world!</string>
<string name="toast">Toast!</string>
<string name="menu_settings">Settings</string>
</resources>
view raw strings.xml hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿