2013年8月22日 星期四

「Android 程式」 Button按下執行Toast

package com.example.updata;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;//元件
import android.widget.Button;
public class Ud extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ud);
//按鍵按下去顯示訊息的方法
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "顯示toast", Toast.LENGTH_LONG).show();
                     
                       //text使用getApplicationContext()取得目前的text;getContext()取得目前執行按鍵的元件
                       //Toast.LENGTH_LONG設定訊息停留的時間長短
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ud, menu);
return true;
}
}


沒有留言:

張貼留言

影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。