How to create custom alert dialog in android?

前端 未结 9 1209
再見小時候
再見小時候 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.

     <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="56dp"
            android:text="Popup Window will display on this Activity" />
    
        <Button
            android:id="@+id/popupbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Show Popup" />
    
    </RelativeLayout>
    

    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();
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-09 11:03

    its working for me

    Dialog m_dialog; 
    m_dialog = new Dialog(BusinessDetail.this);
                m_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                
                LayoutInflater m_inflater = LayoutInflater.from(BusinessDetail.this);
                View m_view = m_inflater.inflate(R.layout.rateus_popup, null);
                myPopLay = (LinearLayout) m_view.findViewById(R.id.myPopLay);
    m_dialog.setContentView(m_view);
                m_dialog.show();
    
    0 讨论(0)
  • 2020-12-09 11:06

    you can use Dialog , like this code :

    final Dialog dialog = new Dialog(context);
    // dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom);
    dialog.setTitle("Title...");
    
    // set the custom dialog components - text, image and button
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Android custom dialog example!");
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    dialog.dismiss();
    }
    });
    dialog.show();
    

    if you want to remove title bar just use this code after define :

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    
    0 讨论(0)
提交回复
热议问题