0

我的问题是我有一个弹出窗口,当弹出窗口关闭时,它不保存编辑文本的输入。预期结果:我想要的是当弹出窗口关闭时,如果出于任何原因我需要重新打开弹出窗口,我希望看到我放置的值。我尝试过修改活动生命周期和 onSaveInsanceState 方法,但我的问题似乎没有得到解决。出于某种原因,我的弹出窗口有它自己的生命。它基本上每次都会创建一个新的 popUp 实例。

            public class MainActivity extends Activity {
                 private Point p;
                 private Button b;
                 private TextView tv;
                 private PopupWindow popup;
                 private LayoutInflater layoutInflater;
                 private View layout;
                 private LinearLayout viewGroup;
                 private EditText etext;
           //on create method
                  @Override
             protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
            mainViewItems();
            onPause();
         }
              //allows me to pause session
                  @Override
              protected void onPause(){
        super.onPause();
            mainViewItems();

               }

                  // This method is in charge of instantiating the          
                 // buttons,textviews,  and allowing an event handler to 
                 // call     
                showPopup() method.
               public void mainViewItems(){
           b= (Button) findViewById(R.id.button1);
           tv = (TextView) findViewById(R.id.textView1);
                   b.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {   
                     showPopup(MainActivity.this,p);}});

                }

         @Override
        public void onWindowFocusChanged(boolean hasFocus) {

       int[] location = new int[2];
       b.getLocationOnScreen(location);
       //Initialize the Point with x, and y positions
       p = new Point();
       p.x = location[0];
       p.y = location[1];

    }






    // The method that displays the popup.
    private void showPopup(final Activity context,Point p) {
       int popupWidth = 230;
       int popupHeight = 190;

       // Inflate the popup_layout.xml
     viewGroup = (LinearLayout) context.findViewById(R.id.pop);
     layoutInflater = (LayoutInflater)  
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     layout = layoutInflater.inflate(R.layout.popup_layout, 
                                                                viewGroup);

     // Creating the PopupWindow
       popup = new PopupWindow(layout,350,350,  true); //context
       popup.setContentView(layout);//layout
       popup.setWidth(popupWidth);
       popup.setHeight(popupHeight);
       popup.setFocusable(true);
       popup.setTouchable(true);
       popup.setOutsideTouchable(false);
       popup.update();


      int OFFSET_X = -100;
      int OFFSET_Y = 100;


       // Displaying the popup at the specified location, + offsets.
         popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x +    
                                OFFSET_X, p.y + OFFSET_Y);

      etext = (EditText) layout.findViewById(R.id.editText1);
      Button  b2 = (Button) layout.findViewById(R.id.calculate);

       b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v)    
                     {tv.setText(etext.getText().toString());}});
       Button close = (Button) layout.findViewById(R.id.close);


            close.setOnClickListener(new OnClickListener() {

           @Override
           public void onClick(View v) { popup.dismiss(); }});

                  }


                      }
4

1 回答 1

0

每次显示它时,您都会创建一个新的 PopupWindow。您还能期待什么 - 它是一个带有新 EditText 的新窗口。解决此问题的最简单方法是将最后输入的字符串存储在活动的成员变量中,并在显示弹出窗口时将编辑文本设置为此变量。

于 2013-05-16T06:13:34.277 回答