2015-04-06 47 views
-2

我正在创建我的第一个Android应用程序,我需要一些帮助,使用按钮创建一个简单的通知。一旦按下按钮,我想在设定的时间后通知自定义消息。无论用户在做什么,我都希望通知能够显示。非常感谢你在Android应用程序中创建通知

回答

0

在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>