带有button的Android Eclipsepopup消息

我想这样做,通过点击一个button,有一个popup消息。

现在popup来,只要我打开应用程序。

顺便说一下,我想要触发popup的button是main.xml中的aboutbutton

这是我的main.xml(与布局的东西):

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#3DE400" android:orientation="vertical" > <!-- background originally #d78a00 --> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:fontFamily="sans-serif-condensed" android:paddingLeft="10dp" android:text="Sample App" android:textColor="#FFF" android:textSize="60sp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="sans-serif-condensed" android:paddingLeft="10dp" android:text="@string/creator" android:textColor="#FFF" android:textSize="20dp" /> <Button android:id="@+id/about" android:layout_width="123dp" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:background="@android:color/transparent" android:fontFamily="sans-serif-condensed" android:gravity="left" android:paddingLeft="10dp" android:text="@string/about" android:textColor="#FFF" android:textSize="40dp" android:onClick="show" /> </LinearLayout> 

这是我的MainActivity.java

 package com.pranavsanghvi.sampleappv4; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.view.Menu; import android.widget.Toast; import android.content.DialogInterface; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("About"); alert.setMessage("Sample About"); alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick (DialogInterface dialog, int id) { Toast.makeText (MainActivity.this, "Success", Toast.LENGTH_SHORT) .show(); } }); alert.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT) .show(); } }); alert.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

Solutions Collecting From Web of "带有button的Android Eclipsepopup消息"

首先,在MainActivity中声明你的警报和button:

 public class Mainactivity extends Activity { private AlertDialog.Builder alert; private Button btAbout; //rest of the code } 

然后,在onCreate()中,像以前一样创build警报,除了这一行:

 alert.show(); // <--- remove this line as not to show the alert immediately 

由于您声明了全局警报,因此请记得在此处删除AlertDialog.Builder,而不是:

 AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("About"); //rest of the code 

你应该有:

 alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("About"); //rest of the code 

接下来,获取关于button的句柄:

 btAbout = (Button) findViewById(R.id.about); 

将onClickListener设置为button:

 btAbout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //when this button is clicked, show the alert alert.show(); } }); 

所有这一切都在onCreate()中。 现在当button被点击时,你的警报将被显示。

如果你想显示关于button的popup窗口,请在onCreate()添加以下内容

 Button aboutButton = (Button) findViewById(R.id.about); aboutButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { alert.show(); } }); 

只要删除alert.show(); 来自onCreate();

更新: –

你是否获得警报无法解决 ? 如果是这样,那么要么警惕全球即宣布外onCreate()

 public class MainActivity extends Activity { AlertDialog.Builder alert; @Override protected void onCreate(Bundle savedInstanceState) { // code alert = new AlertDialog.Builder(MainActivity.this); // code 

或者使其最终成为现实

 final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 

还删除alert.show(); 这是在onCreate();