android如何到主要从弹出式菜单中检索值?

发布时间: 2016/9/16 13:13:35
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个主要 Activity 调用一个弹出式窗口, window :

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));
}

}

XML 的 poup window 是︰

<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 和把他们送回父。 如何可以发送的数据拷贝回 view 谁叫弹出窗口?

解决方法 1:

您可以使用 startActivityForResult 和 setResult 的方法来实现这一目标。 在 MainActivity 开始弹出 activity 喜欢这

 startActivityForResult(popup,1);

在弹出窗口 activity 使用 setResult 方法将值传递回主 activity

    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");
官方微信
官方QQ群
31647020