Java 带按钮的 Android Eclipse 弹出消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21221054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Eclipse Popup Message with Button
提问by user3212831
I would like to make it so, by a click of a button, there is a popup message.
我想让它这样,通过点击一个按钮,有一个弹出消息。
Right now the popup comes as soon as I open the app.
现在,只要我打开应用程序,就会弹出窗口。
BTW the button I want to trigger the popup is the about button in main.xml
顺便说一句,我想触发弹出的按钮是 main.xml 中的 about 按钮
Here is my main.xml (with the layout stuff):
这是我的 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>
Here is my MainActivity.java:
这是我的 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;
}
}
回答by Rohan Kandwal
If you want to show popup on about button click add the following in onCreate()
如果要在关于按钮上显示弹出窗口,请单击添加以下内容 onCreate()
Button aboutButton = (Button) findViewById(R.id.about);
aboutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alert.show();
}
});
just remove alert.show();
from onCreate();
只需alert.show();
从 onCreate() 中删除;
update :-
更新 :-
Are you getting alert can't be resolved? If so then either make alert global i.e. declare it outside onCreate()
您是否收到警报无法解决?如果是这样,要么使警报成为全球性的,即在外面声明它onCreate()
public class MainActivity extends Activity {
AlertDialog.Builder alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
// code
alert = new AlertDialog.Builder(MainActivity.this);
// code
or make it final so that it is
或使其最终成为
final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
also remove alert.show();
which is in onCreate();
也删除alert.show();
哪个在onCreate();
回答by Melquiades
First, declare your alert and button in MainActivity:
首先,在 MainActivity 中声明您的警报和按钮:
public class Mainactivity extends Activity {
private AlertDialog.Builder alert;
private Button btAbout;
//rest of the code
}
Then, in onCreate(), create your alert as you did, except this line:
然后,在 onCreate() 中,像您一样创建警报,除了这一行:
alert.show(); // <--- remove this line as not to show the alert immediately
Because you've declared global alert, remember to also remove AlertDialog.Builder here, so instead of:
因为你已经声明了全局警报,记得在这里也删除 AlertDialog.Builder,而不是:
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("About");
//rest of the code
you should have:
你应该有:
alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("About");
//rest of the code
Next, get the handle to your about button:
接下来,获取关于按钮的句柄:
btAbout = (Button) findViewById(R.id.about);
Set onClickListener to button:
将 onClickListener 设置为按钮:
btAbout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//when this button is clicked, show the alert
alert.show();
}
});
All of this is in onCreate(). Now when button is clicked, your alert will be displayed.
所有这些都在 onCreate() 中。现在,当单击按钮时,将显示您的警报。