0

我想要一个弹出窗口以随机或按顺序显示数据库中存在的 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;

    }
}

这段代码对我有帮助吗?

4

2 回答 2

0

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

  • 吐司
  • 警报对话框

吐司:

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

对话:

它会显示您的消息,但在用户按下按钮(例如“确定”按钮)之前不会离开屏幕。

吐司:

 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();
                    }
                });
于 2013-08-03T15:49:01.437 回答
0

Toast 是最简单的:

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

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

于 2013-08-03T15:37:24.973 回答