如何从弹出窗口中检索值到主?

时间:2016-02-08 07:31:17

标签: android android-studio popupwindow

我有一个调用弹出窗口的主Activity

AddProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent popup = new Intent(SingleVoucher.this, PopUp.class);
                if (additionalProduct!=null) {
                    popup.putExtra("additionalamount", additionalQuantity);
                    popup.putExtra("additionalprice", additionalPrice);
                    popup.putExtra("additionalproduct", additionalProduct);
                }
                startActivity(popup);

            }
        });

弹出窗口:

public class PopUp extends Activity {

private OnSubmitListener mListener;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.popup_window);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    WindowManager.LayoutParams windowManager = getWindow().getAttributes();
    windowManager.dimAmount = 0.75f;
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    TextView SubmitAdditional = (TextView) findViewById(R.id.SubmitAdditional);
    final EditText product = (EditText) findViewById(R.id.adittionalTextEdit);
    final EditText qty = (EditText) findViewById(R.id.additionalQuantityTE);
    final EditText price = (EditText) findViewById(R.id.adittionalPriceTE);
    final Intent intent = getIntent();
    final Context context = this;
    SubmitAdditional.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String additionalProduct = product.getText().toString();
            String additionalQuantity = qty.getText().toString();
            String additionalPrice = price.getText().toString();
            if (additionalProduct!=null && additionalPrice.isEmpty()==false && additionalQuantity.isEmpty()==false) {
                intent.putExtra("additionalamount", additionalQuantity);
                intent.putExtra("additionalprice", additionalPrice);
                intent.putExtra("additionalproduct", additionalProduct);

            }
            else{
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Missing Details");
                alert.setMessage("Some of the required fields are missing. please try again");
                alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });


            }

        }
    });
    getWindow().setLayout((int) (width * .8), (int) (height * .3));
}

}

并且poup窗口的XML是:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/BlueGray">

    <TextView
        android:id="@+id/AdditionalTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="5dp"
        android:layout_alignParentTop="true"
        android:paddingTop="2dp"
        android:text="Product Name"
        android:textSize="18dp"
        android:textColor="@color/White">
    </TextView>

    <EditText
        android:id="@+id/adittionalTextEdit"
        android:layout_width="260dp"
        android:layout_height="50dp"
        android:layout_below="@+id/AdditionalTitle"
        android:background="@color/White"
        android:textSize="12dp"
        android:textColor="@color/Black"
        android:layout_centerHorizontal="true"
        android:gravity="top">
        </EditText>

    <TextView
        android:id="@+id/additionalQuantityTV"
        android:layout_marginLeft="12dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/adittionalTextEdit"
        android:text="Quantity"
        android:textColor="@color/White"
        android:textSize="15dp"
        android:layout_marginTop="5dp">
    </TextView>

    <EditText
        android:id="@+id/additionalQuantityTE"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:layout_toRightOf="@+id/additionalQuantityTV"
        android:layout_below="@+id/adittionalTextEdit"
        android:background="@color/White"
        android:layout_marginTop="5dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="14dp"
        android:layout_marginLeft="38dp"
        android:numeric="integer"
        android:textSize="14dp">
        </EditText>

    <TextView
        android:id="@+id/adittionalPriceTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_below="@+id/additionalQuantityTV"
        android:text="Overall price"
        android:textColor="@color/White"
        android:textSize="15dp"
        android:layout_marginTop="5dp">
        </TextView>

    <EditText
        android:id="@+id/adittionalPriceTE"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:layout_toRightOf="@+id/adittionalPriceTV"
        android:layout_below="@+id/additionalQuantityTE"
        android:background="@color/White"
        android:layout_marginTop="5dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="14dp"
        android:layout_marginLeft="10dp"
        android:numeric="decimal"
        android:textSize="14dp">
</EditText>

    <TextView
        android:id="@+id/popupClose"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_below="@+id/adittionalPriceTE"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="14dp"
        android:textColor="@color/White">
    </TextView>

    <TextView
        android:id="@+id/SubmitAdditional"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_below="@+id/adittionalPriceTE"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="14dp"
        android:textColor="@color/White">
        </TextView>
</RelativeLayout>    

我想从EditText获取值并将它们发送回父级。 如何将数据发送回调用弹出窗口的视图?

3 个答案:

答案 0 :(得分:2)

您必须startActivityForResult()

然后使用PopUp

setResult()活动返回结果

看到这个好link for statActivityForResult

答案 1 :(得分:2)

您可以使用startActivityForResult和setResult方法来实现此目的。 在MainActivity中启动像这样的弹出活动

 startActivityForResult(popup,1);

在弹出活动中,使用setResult方法将值传递回主活动。

    Intent intent = new Intent();
    intent.putExtra("data",data); // data is the value you need in parent
    setResult(100,data);

在MainActivity中使用onActivityResult方法获取数据

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     value = data.getBundleExtra("data");

答案 2 :(得分:0)

尝试使用startActivityForResult来显示弹出窗口。

以下是一个示例:http://developer.android.com/training/basics/intents/result.html

然后在主活动中,您应该覆盖onActivityResult,以便访问您从弹出窗口发送的信息。