2014-01-19 9 views
8

Ich möchte es so machen, durch einen Klick auf eine Schaltfläche, gibt es eine Popup-Nachricht.Android Eclipse Popup Nachricht mit Button

Im Moment kommt das Popup, sobald ich die App öffne.

BTW die Schaltfläche Ich möchte das Popup auszulösen ist die über Schaltfläche in main.xml

Hier mein main.xml ist (mit dem Layout Sachen):

<?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> 

Hier ist meine 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; 
    } 

} 

Antwort

0

Wenn Sie Popup auf etwa Schaltfläche angezeigt werden sollen folgend klicken hinzufügen in onCreate()

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

entfernen Sie einfach alert.show(); von onCreate();

Update: -

Sind Sie Benachrichtigung bekommen kann nicht gelöst werden? Wenn ja dann entweder Alarm global machen also erklären sie außerhalb onCreate()

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

oder es endgültig machen, so dass es

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

auch alert.show(); entfernen, die in ist onCreate();

+0

wenn ich das tue, habe ich zwei nicht gelöst werden kann auf die alarmierung und auf den addNewContact .. wie kann ich das beheben? – user3212831

+0

Verwenden Sie 'alert.show()' nur innerhalb 'aboutbutton's' setOnClickListener' –

+0

siehe mein update @ user3212831 –

0

Zuerst erklären Ihre Warnung und Taste in HauptAktivität:

public class Mainactivity extends Activity { 
    private AlertDialog.Builder alert; 
    private Button btAbout; 

    //rest of the code 
} 

Dann in onCreate(), erstellen Sie Ihre Warnung, wie Sie außer dieser Linie haben,:

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

Weil Sie globalen Alarm erklärt haben, denken Sie daran, auch AlertDialog.Builder hier, also statt zu entfernen:

AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
alert.setTitle("About"); 

//rest of the code 

sollten Sie haben:

alert = new AlertDialog.Builder(MainActivity.this); 
alert.setTitle("About"); 

//rest of the code 

Als nächstes wird der Griff auf Ihre über Schaltfläche erhalten:

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

Set OnClickListener auf Taste:

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

All dies ist in onCreate(). Wenn Sie nun auf die Schaltfläche klicken, wird Ihre Warnung angezeigt.

+0

alles funktioniert gut außer in "alert.show();" in "btAbout.setOnClickListener (neue Ansicht.OnClickListener() {"Die Warnung enthält einen Fehler, der besagt" Kann nicht auf eine nicht endgültige Variablenwarnung innerhalb einer durch eine andere Methode definierten inneren Klasse verweisen " – user3212831

+0

keine Ahnung, wie Sie das beheben können? – user3212831

+0

Haben Sie in MainActivity 'alert' deklariert, nicht in onCreate()? – Melquiades