android的弹出代码

时间:2013-08-03 15:30:11

标签: android android-layout

我希望弹出窗口以随机或顺序显示数据库中存在的d字符串数据之一。我应该使用什么代码用于这种弹出窗口。我是Android开发的新手。我很感激帮助。请不要留下这个答案。

package popupTest.popupTest;

import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class popupTest extends Activity {

 PopupWindow popUp;
 LinearLayout layout;
 TextView tv;
 LayoutParams params;
 LinearLayout mainLayout;
 Button but;
 boolean click = true;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  popUp = new PopupWindow(this);
  layout = new LinearLayout(this);
  mainLayout = new LinearLayout(this);
  tv = new TextView(this);
  but = new Button(this);
  but.setText("Click Me");



  params = new LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT);
  layout.setOrientation(LinearLayout.VERTICAL);
  tv.setText("Hi this is a sample text for popup window");
  layout.addView(tv, params);
  popUp.setContentView(layout);
  // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
  mainLayout.addView(but, params);
  setContentView(mainLayout);

    Handler handler = new Handler();
      handler.postDelayed(new Runnable(){

        public void run() {
            // TODO Auto-generated method stub
            popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
             popUp.update(50, 50, 300, 80);
        }

      }, 1000);
  //Use this to dismiss as per your need...
    // popUp.dismiss();

 }
 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         popUp.dismiss();
        return false;

    }
}

这段代码会帮助我吗?

2 个答案:

答案 0 :(得分:0)

Toast是最简单的:

Toast.makeText(getApplicationContext(), "Button is clicked", Toast.LENGTH_LONG).show();

示例:http://www.mkyong.com/android/android-toast-example/

答案 1 :(得分:0)

为了弹出一些信息,有两种最常用的方法。

  • 吐司
  • AlertDialog

<强>吐司:

它出现一段特定的时间,显示一些信息然后消失。

<强>对话框:

它显示您的消息,但不会离开屏幕,直到用户按下按钮,例如“确定”按钮。

祝酒:

 Toast.makeText(getApplicationContext(), "your message here", Toast.LENGTH_LONG).show();

对话:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());

 alertDialogBuilder.setTitle("Your Title");

 alertDialogBuilder.setMessage("This is to notify you");

 alertDialogBuilder.show();
 alertDialogBuilder.setButton("OK", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog,int which) 
                    {
                        // Write your code here to execute after dialog closed
                    Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
                    }
                });