将按钮动态添加到Android弹出窗口

时间:2013-06-07 00:23:10

标签: android

我需要帮助在弹出窗口中创建4个EditTexts的动态数组。如果弹出窗口是它自己的活动,弹出窗口与动态EditText的实现将很有效;但是,弹出窗口不应该拥有它自己的活动,而是使用它所在的类的活动。你能。请详细说明一下。我相信这必须处理应用程序上下文。这是代码。

public class SideBucket extends Activity {

//The "x" and "y" position of the "Show Button" on screen.
            private Point p;
            private String[] _hold;
            private LinearLayout viewGroup;
            private TextView tv;
            private View layout;
            private EditText[] _edText;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_side);
           tv = (TextView) findViewById(R.id.textView1);
    this.buttonActions();//explicit call to this class.
          onPause();
  }

     @Override
         protected void onPause(){
    super.onPause();
           buttonActions();
      }

    public void buttonActions(){
    // Array for buttons that initially show up on the main Activity.
    final Button[] b = {(Button) findViewById(R.id.button1),  (Button)        findViewById(R.id.side_next)};

    for(int i=0;i<b.length;i++){
        b[i].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                 //this button is in the main activity  
                    and when pressed the popped out window shows up.
                if(b[0].isPressed()){ 
                    //Open popup window
                    if(p !=  null)   

                                {showPopup(SideBucket.this, p);}

             //for(int t=0;t<_edText.length;t++){

                    //_edText[t].setText(_hold[t]);
                    //}

                }//end of if statement              

                 // this button is in the main activity   
                 of the sideBucket mxl file and it switches intents.
                if(b[1].isPressed()) {
                    Intent intent = new 
                       Intent(SideBucket.this,FrontBucket.class);
                    startActivity(intent);
                }
                //end for view v
               }});
            }//for loop
              }

     // Get the x and y position after the button is draw on screen
    // (It's important to note that we can't get the position in  the    onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))

                 @Override
                 public void onWindowFocusChanged(boolean hasFocus) {

    int[] location = new int[2];
    Button button = (Button) findViewById(R.id.button1);

         // Get the x, y location and store it in the location[] array
          // location[0] = x, location[1] = y.
             button.getLocationOnScreen(location);
           //Initialize the Point with x, and y positions
    p = new Point();
    p.x = location[0];
    p.y = location[1];
            }   


          public void showPopup(final Activity context, Point p) {
      int popupWidth = 230;
      int popupHeight = 330;

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

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

//===============================//



            //sets dimensions
          LayoutParams param = new    LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
              //goal how can we get layout to view the editText
              _edText = new EditText[4];
            for (int i = 0;i< _edText.length;i++) {
            _edText[i] = new EditText(getApplicationContext());
    btn[i].setText(btn[i].toString());
//   _edText[i].setTextColor(Color.parseColor("#000000"));// why is the color parsed


       _edText[i].setLayoutParams(param);

        viewGroup.addView(_edText[i]);

        viewGroup.addView(_edText[i]);

          }

         // Creating the PopupWindow
         final PopupWindow popup = new PopupWindow(layout,350,350,true);  //               context argument was taken out of this method.
         popup.setContentView(layout);
         popup.setWidth(popupWidth);
         popup.setHeight(popupHeight);
         popup.setFocusable(true);



      // Some offset to align the popup a bit to the right, and a bit  
          down, relative to button's position.
      int OFFSET_X = -100;
       int OFFSET_Y = 100;

      // Clear the default translucent background
      popup.setBackgroundDrawable(new BitmapDrawable());

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

      //Refference to editText in popup in an array.
        // this array should be changed later on to create something more
        dynamically   robust.


//========================================================//


           //Button calculate = (Button) layout.findViewById(R.id.close_calc);
       //button inside of pop-up layout.
      //calculate.setOnClickListener(new OnClickListener() {

      //    @Override
      //    public void onClick(View v) {
     // _hold = new String[4];
    //  for(int i=0;i<_hold.length;i++){

    //      _hold[i] = _edText[i].getText().toString();
    //  }
    //  tv.setText("Result1:"+_hold[0]+"\n"+"Result2:"+_hold[1]+"
                \n"+"Result3:"+_hold[2]+"\n"+"Result4:"+_hold[3]);
    //  popup.dismiss();
    //}
      //});

         }
}

0 个答案:

没有答案