コード例

Android用語集

カテゴリー: Toastクラス  閲覧数:371 配信日:2014-03-20 16:51


例1


▼/HelloAndroid/src/android/style/HelloAndroidActivity4.java
package android.style;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

public class HelloAndroid4Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Contextの取得
Context context = getApplicationContext();
// Toastオブジェクトの生成
Toast toast = Toast.makeText(context,
"Toastサンプル", Toast.LENGTH_LONG);
// 表示
toast.show();

// 以下のようにメソッドチェーンを利用して1行にすることもできる
// Toast.makeText(this, "Toastサンプル", Toast.LENGTH_LONG).show();

}
}


・xml … レイアウト
▼/HelloAndroid/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

</LinearLayout>