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(); }});
}
}
感謝您的意見。我忘了提及我實際上做了這個以及我的一個嘗試;但是,我沒有將etext.setText(變量)放在正確的方法中。這次我把它放在showPopUp()方法下的mainViewItemsMethod()中。 – HRo