Java 我想从弹出窗口访问返回调用弹出窗口的活动的值
我有一个活动,我有一个按钮,将导致在同一活动弹出窗口。在弹出窗口中,我有多个字段。我想要的是把这些值从弹出窗口返回到同一个活动中 我一直被它缠住了。需要帮助:) 相关代码如下。 这是onCreate方法中用于调用弹出窗口的代码Java 我想从弹出窗口访问返回调用弹出窗口的活动的值,java,android,popup,Java,Android,Popup,我有一个活动,我有一个按钮,将导致在同一活动弹出窗口。在弹出窗口中,我有多个字段。我想要的是把这些值从弹出窗口返回到同一个活动中 我一直被它缠住了。需要帮助:) 相关代码如下。 这是onCreate方法中用于调用弹出窗口的代码 Button button = (Button) findViewById(R.id.button9); button.setOnClickListener(new View.OnClickListener() { @Override
Button button = (Button) findViewById(R.id.button9);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addProductDetails();
}
});
通过调用addProductDetails()方法,将显示弹出窗口
所以在这个方法中,代码如下
private String addProductDetails() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.storeproductdetails_layout, (ViewGroup) findViewById(R.id.popup_element1), false);
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//get txt view from "layout" which will be added into popup window
//before it you tried to find view in activity container
/* Field Data */
product1Code = (EditText) layout.findViewById(R.id.productCodeee);
quantity1 = (EditText) layout.findViewById(R.id.quantityy);
details1 = (EditText) layout.findViewById(R.id.editText23);
orderValue1 = (EditText) layout.findViewById(R.id.editText36);
productC = String.valueOf(product1Code.getText());
qty = String.valueOf(quantity1.getText());
dts = String.valueOf(details1.getText());
orderVal = String.valueOf(orderValue1.getText());
StringBuffer sb = new StringBuffer("");
sb.append(productC+","+qty+","+dts+","+orderVal);
System.out.println("StringBuffer Value: "+sb.toString());
/* End of the field Data */
Button doneAddingProduct = (Button) layout.findViewById(R.id.doneAddingProduct);
//init your button
Button btnClosePopup = (Button) layout.findViewById(R.id.closePopup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
doneAddingProduct.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Inside onclick of done adding");
Product product = new Product();
product.setProductCode(productC);
product.setQuantity(qty);
product.setDetails(dts);
product.setOrderValue(orderVal);
app.setProduct(product);
Intent intent = new Intent(OrderReturnMgmtSecondActivity.this, OrderReturnMgmtSecondActivity.class);
startActivity(intent);
}
});
//show popup window after you have done initialization of views
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
return sb.toString();
}
在这里,我尝试使用两种方法将这些字段数据提取回活动。1) 简单地尝试通过将字段数据作为字符串连接到调用此方法的人来返回字段数据 2) 创建一个名为Product(包含字段数据)的pojo,并将其设置在应用程序类中,即MyApplication extends application(您可以看到代码为app.setProduct(Product)),将其重定向到同一活动,并尝试从该应用程序获取数据 但我仍然无法获得这些现场数据 任何帮助都将不胜感激:)创建一个界面
interface Listener {
void onResult(Product product);
}
addProductDetails(new Listener(){
void onResult(Product product){
//do something with product
}
});
将侦听器传递给该方法并调用它
addProductDetails(Listener listener){
//...
//when finished:
listener.onResult(product);
//...
}
调用方现在必须实现接口
interface Listener {
void onResult(Product product);
}
addProductDetails(new Listener(){
void onResult(Product product){
//do something with product
}
});
如何在应用程序中设置数据?我正在清单文件中配置自定义应用程序类。这样应用程序类中的数据将在整个应用程序中可见。如果需要,我可以在任何地方或任何活动中使用它。