Dans mon application, j'ai un bouton initialement sur l'écran, et dans onclick
du bouton, une fenêtre contextuelle devrait s'ouvrir. Dans la fenêtre contextuelle, j'ai un bouton image, et onclick
de ce bouton, je veux démarrer une activité. La fenêtre contextuelle s'ouvre, mais je ne comprends pas comment gérer le onclick
du bouton d'image dans la fenêtre contextuelle.comment gérer onclick événement de bouton dans la fenêtre contextuelle dans android
Dans main.xml, j'ai un bouton, et dans popup_example.xml, j'ai un bouton image.
code Java Mon est la suivante:
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Button b=(Button)findViewById(R.id.btn);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main)));
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});
//if onclick is written here it gives runtime exception.
});
et j'ai deux mises en page XML .........
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ghj" /> </LinearLayout>
popup_example.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#8E2323"> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px"> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px" android:background="#000000"> <ImageButton android:id="@+id/home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="true" android:src="@drawable/vitalss" android:layout_weight="1" android:background="#8E2323"/> </TableLayout> </TableLayout> </LinearLayout>
hey merci beaucoup !! Cela marche. – henna