-2
我正在創建我的第一個Android應用程序,我需要一些幫助,使用按鈕創建一個簡單的通知。一旦按下按鈕,我想在設定的時間後通知自定義消息。無論用戶在做什麼,我都希望通知能夠顯示。非常感謝你在Android應用程序中創建通知
我正在創建我的第一個Android應用程序,我需要一些幫助,使用按鈕創建一個簡單的通知。一旦按下按鈕,我想在設定的時間後通知自定義消息。無論用戶在做什麼,我都希望通知能夠顯示。非常感謝你在Android應用程序中創建通知
在android中有一種叫做toast的方法。
在按鈕單擊事件Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();// popup keep long time
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_SHORT).show();//popup keep short time
由於您選擇初學者,我會提供完整的代碼。
//你mainactivity
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Toast method
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
});
}
}
//In your 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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button - Go to mkyong.com" />
</LinearLayout>