2. MainActivity.java
package org.me.android_toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button toastButton;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
toastButton = (Button) findViewById(R.id.toastButton);
//設定按鈕的ClickListener
toastButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
//當使用者按下按鈕時顯示Toast
//Toast.LENGTH_LONG表示顯示時間較長,Toast.LENGTH_SHORT則表示顯示時間較短
Toast.makeText(view.getContext(), "這是一個Toast......", Toast.LENGTH_LONG).show();
}
});
}
}
3. main.xml(Layout)
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/toastButton"
android:layout_width="90px"
android:layout_height="37px"
android:text="ShowToast">
</Button>
</LinearLayout>
4. 按下ShowToast按鈕之後出現的畫面






沒有留言:
張貼留言