2016-04-16 1 views
0

Donc, ayant un peu de mal avec une boîte popup, nouveau pour Android et ne sais pas comment s'y prendre. Je voudrais avoir un popup qui affiche le texte spécifique basé sur une variable publique, ainsi quand le bouton est cliqué, la variable est vérifiée, et selon ce que cette variable est le texte approprié est affiché dans le textView.Fenêtre popup Android avec texte variable

  • Est-il possible de créer des variables chaîne, et avec une série d'instructions if transmettre ces variables à textView? Ai-je besoin de fichiers de mise en page individuels pour chacun et utiliser les instructions if pour déterminer quelle vue sera transmise et affichée?

popup.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <TextView 
      android:id="@+id/text_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="" /> 

    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Close" 
      android:id="@+id/button1"/>  

</LinearLayout> 

mainActivity.java

Button pubtn = (Button)findViewById(R.id.popupOpen); 
pubtn.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 

    View puView = layoutInflater.inflate(R.layout.popup, null); 
    PopupWindow puWindow = new PopupWindow(
       puView, 
       LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT); 

      Button btnExit = (Button)puView.findViewById(R.id.button1); 
      btnExit.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View v) { 
     puWindow.dismiss(); 
    }}); 
      puWindow.showAsDropDown(pubtn, 0, 0); 
    }}); 

toute aide ou des directives sur la façon d'aller à ce sujet serait très apprécié

Répondre

1

Vous pouvez utiliser cet exemple :

LayoutInflater inflater = (LayoutInflater) YourActivity.this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.popup_xml, 
      (ViewGroup) findViewById(R.id.popup_element)); 
    popupWindow = new PopupWindow(layout, 300, 190, true); 

    popupText = (TextView) layout.findViewById(R.id.popupText); 
    popupText.setText(yourString); 
+0

Brillant! Si simple, je ne sais pas pourquoi je n'ai pas pensé à ça. – Abstract3000

+0

Encore une chose, j'ai vu dans API 21 que vous ne pouvez pas fermer la popup, donc vous devriez ajouter ceci: popupWindow.setBackgroundDrawable (new BitmapDrawable()); –