如何在android的弹出窗口中设置Activity?

时间:2016-08-02 11:23:10

标签: android android-activity

我想在弹出窗口中设置活动内容.. 就像我想在活动中提到设置按钮文本一样。 这是我的代码和他们各自的文件。

popupscreen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/popup_element"
    android:background="#7000">


<Button
    android:id="@+id/btn_close_popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
     />

</LinearLayout> 

PopupScreen.java

public class PopupScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_popup);
    Button bt=(Button)findViewById(R.id.btn_close_popup);
    bt.setText("close this");
    }

  // And this is Popup window code in MainActivity

   private PopupWindow pwindo;
   private void opPopupWindow(){
    try {

        LayoutInflater inflater = (LayoutInflater)MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_popup,
                (ViewGroup) findViewById(R.id.popupscreen));

        pwindo = new PopupWindow(layout, 400, 470, true);
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

        btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
        btnClosePopup.setOnClickListener(cancel_button_click_listener);

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

现在我想在PopupScreen.Java

中设置Button文本

1 个答案:

答案 0 :(得分:1)

在PopupScreen.java中创建一个DialogFragment,如下所示,

public class PopupScreen extends DialogFragment {
    public static String TAG = PopupScreen.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup 
             container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.screen_popup, container, false);
    }
}

然后从您想要拥有popupScreen的Activity中调用它。

PopupScreen popupScreen = new PopupScreen();
popupScreen.show(getFragmentManager(), PopupScreen.TAG);