如何在按钮单击时调用或弹出片段

How to call or pop up fragment upon button click

我正在实施一个 android 应用程序。我想在点击评级按钮时调用或弹出片段。

例如,这里附上截图。当我点击评分按钮时,如何像这样调用弹出窗口或片段

请帮我如何调用这些片段?

你需要另外设计一个layout,只为pop up显示,并且alignparenttop。这是示例布局

新建 xml 文件 dialogue.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLyt"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#FFFFFF"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Fill in your design in this place"
            android:textSize="10dp" />


    </RelativeLayout>

</RelativeLayout>

创建设计后。您需要在点击按钮时调用它。以下是显示对话的示例

ratingButton.setOnCLickListener(new view.OnCLickListener(){ 
    @Override 
    public void onCLick(View v){
         final Dialog fbDialogue = new Dialog(ProfileActivity.this, android.R.style.Theme_Black_NoTitleBar);
            fbDialogue.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(100, 0, 0, 0)));
            fbDialogue.setContentView(R.layout.facebook_dialogue);
            fbDialogue.setCancelable(true);
            fbDialogue.show();
    } 
}); 


    Hey I have put the code in onclicklistener. Check it . The above code works perfectly fine for me. As per my requirement I wanted the dialogue on the bottom of the screen . I have just made few lines of code change above to meet your requirement. My requirement was something like this   

您可以将其实现为覆盖当前布局。它可能因实施而异。

我使用以下方法实现了这个:

<RelativeLayout>  <- This is the parent 

     <RelativeLayout id="actual_layout"> <- This is where you will implement the layout
           <!--Implement your layout here-->
     </RelativeLayout>

      <RelativeLayout id="rating_container"
      visibility="gone"
      background="#BB000000">  <- This is to set the translucent black background
           <RelativeLayout id="rating_dialog" 
            alignParnetTop="true"> <- This is the rating dialog
                    <!--Your implementation here-->
           </RelativeLayout>
     </RelativeLayout>

</RelativeLayout>

现在在 "Rating" 按钮的 onCLick 中切换 "rating_container" 的可见性。

ratingButton.setOnCLickListener(new view.OnCLickListener(){
    @Override
    public void onCLick(View v){
         toggleRatingContainer(true);
    }
});

您可以按照此方法来实现您的要求。如果您需要更多帮助,请告诉我。

注意:我还没有写完整的代码。另外,我没有检查而是直接输入了所有内容。

编辑:

您可以像下面这样实现 toggleRatingContainer:

public void toggleRatingContainer(boolean showRatingDialog){
    ratingContainer.setVisibility((showRatingDialog)?View.VISIBLE:View.GONE);
}

这里,"ratingContainer" 是 ID 为 "rating_container" 的 "RelativeLayout",您可以在 activity 的 onCreate 方法中获取它。

然后您可以在单击 "Submit" 或 "Cancel" 按钮时将其可见性切换为 "View.GONE",方法是调用传递 "false" 作为参数的相同方法。