2015-12-16 18 views
4

在android中彈出?

我在android開發新的,所以我需要知道這是做彈出這樣的android系統完善,高效的方式,基本上在iOS中很容易使彈出和處理,但是如何下手,這是使用佈局或類似的東西,請參考我的鏈接或指南,以在Android上打開一個活動的彈出窗口。

+0

請參閱https://stackoverflow.com/questions/2115758/how-to-display-alert-dialog-in-android。 – hypd09

+0

這可能有助於瞭解這些想法:https://www.google.com/design/spec/components/dialogs.html#dialogs-simple-dialogs –

回答

1
import android.app.*; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.*; 

public class ShowPopUp extends Activity { 

PopupWindow popUp; 
LinearLayout layout; 
TextView tv; 
LayoutParams params; 
LinearLayout mainLayout; 
Button but; 
boolean click = true; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    popUp = new PopupWindow(this); 
    layout = new LinearLayout(this); 
    mainLayout = new LinearLayout(this); 
    tv = new TextView(this); 
    but = new Button(this); 
    but.setText("Click Me"); 
    but.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      if (click) { 
       popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10); 
       popUp.update(50, 50, 300, 80); 
       click = false; 
      } else { 
       popUp.dismiss(); 
       click = true; 
      } 
     } 

    }); 
    params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    tv.setText("Hi this is a sample text for popup window"); 
    layout.addView(tv, params); 
    popUp.setContentView(layout); 
    // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10); 
    mainLayout.addView(but, params); 
    setContentView(mainLayout); 
     } 
    } 
+0

您可以使用上述代碼創建彈出式窗口。 –

4
private PopupWindow pwindo; 
private void initiatePopupWindow() { 
     try { 
// We need to get the instance of the LayoutInflater 
      LayoutInflater inflater = (LayoutInflater) AndroidAudioPlayer.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.your_layout_Screen, 
        (ViewGroup) findViewById(R.id.popup_element)); 
      pwindo = new PopupWindow(layout, 400, 600, true); 
      pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0); 

      Button btnClosePopup = (Button) layout.findViewById(R.id.button); 
      btnClosePopup.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        pwindo.dismiss(); 
       } 
      }); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

更多時尚和美觀警告對話框, 看看這個庫老古板的Sweet alert dialogs

+0

請確認它是否免費使用。 –

+0

是它的免費,其授權在麻省理工學院許可證https://github.com/pedant/sweet-alert-dialog#license – Veer3383

+0

okkk謝謝親愛的。 –

1

您可以使用PopupWindow

它被定義爲後某些事件發生或某些條款和 條件相匹配的是出現在活動前對 屏幕的窗口。此窗口通常用於顯示一些信息並提供控制來執行操作。彈出窗口 也有自己的GUI,必須在其內容視圖中進行設置。

完整演示PopupWindow in Android

private PopupWindow pw; 
private void initiatePopupWindow() { 
    try { 
     //We need to get the instance of the LayoutInflater, use the context of this activity 
     LayoutInflater inflater = (LayoutInflater) ConfirmActivity.this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     //Inflate the view from a predefined XML layout 
     View layout = inflater.inflate(R.layout.popup_layout, 
       (ViewGroup) findViewById(R.id.popup_element)); 
     // create a 300px width and 470px height PopupWindow 
     pw = new PopupWindow(layout, 300, 470, true); 
     // display the popup in the center 
     pw.showAtLocation(layout, Gravity.CENTER, 0, 0); 

     mResultText = (TextView) layout.findViewById(R.id.server_status_text); 
     Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button); 
     makeBlack(cancelButton); 
     cancelButton.setOnClickListener(cancel_button_click_listener); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private OnClickListener cancel_button_click_listener = new OnClickListener() { 
    public void onClick(View v) { 
     pw.dismiss(); 
    } 
}; 
0

這裏,s可以幫助你: 佈局文件夾中創建一個XML名稱ccc.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffffee" 
android:orientation="vertical" 
android:theme="@style/AppTheme.NoActionBar" 
> 


<ImageView 
    android:layout_width="60dp" 
    android:layout_marginTop="100dp" 
    android:layout_height="60dp" 
    android:layout_gravity="center_horizontal" 
    android:src="@drawable/check" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_marginTop="10dp" 
    android:layout_height="wrap_content" 
    android:text="Invitaton Sent Sucessfully" 
    android:layout_gravity="center_horizontal" 
    android:textStyle="bold" 
    android:textSize="15dp" 
    /></LinearLayout> 

然後寫你想要致電:

b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Dialog d=new Dialog(MainActivity.this); 
      d.setContentView(R.layout.ccc); 
      d.show(); 



     } 
    });