我想在弹出窗口中设置活动的内容。
就像我要在活动中提及的那样设置按钮文字。
这是我的代码及其各自的文件。
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文本
最佳答案
如下所示在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);