【问题标题】:Android Eclipse Popup Message with Button带有按钮的 Android Eclipse 弹出消息
【发布时间】:2014-02-08 20:36:23
【问题描述】:

我想让它这样,通过点击一个按钮,有一个弹出消息。

现在只要我打开应用程序就会弹出窗口。

顺便说一句,我要触发弹出窗口的按钮是 main.xml 中的 about 按钮

这是我的 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;
    }

}

【问题讨论】:

    标签: java android eclipse button popup


    【解决方案1】:

    首先,在 MainActivity 中声明您的警报和按钮:

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

    然后,在 onCreate() 中,像你一样创建警报,除了这一行:

    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
    

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

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

    将 onClickListener 设置为按钮:

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

    所有这些都在 onCreate() 中。现在,当单击按钮时,将显示您的警报。

    【讨论】:

    • 除了“alert.show();”之外,一切都很好在“btAbout.setOnClickListener(new View.OnClickListener() {”中,警报有一个错误,提示“无法引用另一个方法定义的内部类中的非最终变量警报”
    • 知道如何解决这个问题吗?
    • 你是否在 MainActivity 中声明了 alert,而不是在 onCreate() 中?
    • 是的,正如你所说。但我认为问题是因为警报正在 onclicklistener 内部使用,而它是在 mainactivity 外部定义的
    • 你需要把onCreate()中alert变量前面的AlertDialog.Builder去掉,否则,全局alert会被onCreate()中的那个遮住。查看我更新的帖子。
    【解决方案2】:

    如果您想在 about 按钮上显示弹出窗口,请在 onCreate() 中添加以下内容

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

    只需从 onCreate() 中删除 alert.show();

    更新:-

    您是否收到警报无法解决?如果是这样,那么要么将警报设为全局,即在 onCreate() 之外声明它

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

    或者让它成为final,这样它就是

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

    同时删除onCreate();中的alert.show();

    【讨论】:

    • 当我这样做时,我有两个无法解决的警报和 addNewContact.. 我该如何解决这个问题?
    • 只在aboutbuttonsetOnClickListener内使用alert.show()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多