Android-Eclipse-Popup-Meldung mit der Taste
Ich möchte es so machen, durch einen Klick auf eine Schaltfläche, gibt es eine popup-Meldung.
Jetzt das popup kommt sobald ich die app öffnen.
BTW die Taste I auslösen wollen das popup-Menü wird über die Taste in main.xml
Hier ist meine main.xml (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;
}
}
Du musst angemeldet sein, um einen Kommentar abzugeben.
Zunächst deklarieren Sie Ihre Alarm-Taste und in der MainActivity:
Dann in onCreate () ein, erstellen Sie Ihr aufmerksam zu, als Sie haben, außer dieser Zeile:
Weil Sie global deklariert Warnung, denken Sie daran, entfernen Sie auch AlertDialog.Builder hier, also statt:
sollten Sie haben:
Nächsten, sich dem Griff zu Ihr über die Schaltfläche:
Set onClickListener zu Taste:
All dies ist in onCreate(). Nun, wenn die Schaltfläche geklickt wird, wird Ihr Alarm wird angezeigt.
alert
in der MainActivity, nicht in onCreate()?Wenn Sie wollen, um zu zeigen, popup auf die Schaltfläche info klicken, fügen Sie den folgenden in
onCreate()
entfernen Sie einfach
alert.show();
von onCreate();update :-
Sind Sie immer alert kann nicht aufgelöst werden? Wenn ja, dann entweder alert-global, d.h. deklarieren Sie Sie außerhalb
onCreate()
machen oder die final, so dass es
entfernen Sie auch
alert.show();
die inonCreate();
alert.show()
nur innerhalbaboutbutton
'ssetOnClickListener