How to create custom alert dialog in android?

前端 未结 9 1214
再見小時候
再見小時候 2020-12-09 10:36

I am developing a sample app.I am able to show alert on button click having some tittle and button .But now I want to show a pop up window having username (Label)

9条回答
  •  不思量自难忘°
    2020-12-09 11:01

    Here is xml code and activity code see it.

     
        
    
        
    
        

    Activity file

    package com.nkm.popup;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.PopupWindow;
    import android.widget.TextView;
    
    public class PopupDemoActivity extends Activity implements OnClickListener {
    LinearLayout layoutOfPopup;
    PopupWindow popupMessage;
    Button popupButton, insidePopupButton;
    TextView popupText;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        popupInit();
    }
    
    public void init() {
        popupButton = (Button) findViewById(R.id.popupbutton);
        popupText = new TextView(this);
        insidePopupButton = new Button(this);
        layoutOfPopup = new LinearLayout(this);
        insidePopupButton.setText("OK");
        popupText.setText("This is Popup Window.press OK to dismiss         it.");
        popupText.setPadding(0, 0, 0, 20);
        layoutOfPopup.setOrientation(1);
        layoutOfPopup.addView(popupText);
        layoutOfPopup.addView(insidePopupButton);
    }
    
    public void popupInit() {
        popupButton.setOnClickListener(this);
        insidePopupButton.setOnClickListener(this);
        popupMessage = new PopupWindow(layoutOfPopup, LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        popupMessage.setContentView(layoutOfPopup);
    }
    
    @Override
    public void onClick(View v) {
    
        if (v.getId() == R.id.popupbutton) {
            popupMessage.showAsDropDown(popupButton, 0, 0);
        }
    
        else {
            popupMessage.dismiss();
        }
      }
    }
    

提交回复
热议问题