从弹出窗口调用的活动返回一个值

时间:2013-09-04 05:51:01

标签: android popupwindow

我想从弹出窗口调用一个活动,我可以使用方法startActivity()来做,接下来我想将此活动的值传递给弹出窗口中的字段

我试图通过使用startActivityForResult()来传递值,但是我无法覆盖onActivityResult()

我怎么能这样做,还有其他办法吗?

3 个答案:

答案 0 :(得分:1)

那么问题是什么?你想知道如何为一个弹出窗口写一个类吗?那么下面的代码可能有所帮助。按照您想要的方式进行编辑,并为下一个活动编写intent。您还可以通过getintent获取数据,并在整个android活动中通过putextra发送数据。 公共类ShowPopUp扩展了Activity {

PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    popUp = new PopupWindow(this);
    layout = new LinearLayout(this);
    mainLayout = new LinearLayout(this);
    tv = new TextView(this);
    but = new Button(this);
    but.setText("Click Me");
    but.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (click) {
                popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10);
                popUp.update(50, 50, 300, 80);
                click = false;
            } else {
                popUp.dismiss();
                click = true;
            }
        }

    });
    params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    layout.setOrientation(LinearLayout.VERTICAL);
    tv.setText("Hi this is a sample text for popup window");
    layout.addView(tv, params);
    popUp.setContentView(layout);
    // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
    mainLayout.addView(but, params);
    setContentView(mainLayout);
}

}

答案 1 :(得分:0)

您可以使用Bundles将值传递给其他活动。

Intent intent = new Intent(this, SomeActivity.class);
intent.putExtra("someKey", 50);
startActivity(intent);

然后通过执行以下操作在“SomeActivity”类中接收它:

Bundle extras = getIntent().getExtras();
if(extras.containsKey("someKey")){
   int defaultValue = -1;
   Log.d("Magic!", "My bundled extra: " + extras.getInt("someKey", defaultValue)); 
}

答案 2 :(得分:0)

您应该创建宽度和高度有限的透明活动,而不是弹出对话框。因此它看起来像对话框然后你可以打开另一个活动并使用intent.putExtra()

传递值

通过这种方式,您还可以使用startActivityForResult()  和onActivityResult()

public class PopupWindowActivity extends Activity {

PopupWindow popwindow;
Button btnpopupopener,btnOpenActicivt;
TextView tv_result;
final static int POPRESULTcode=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_popup_window);

    btnpopupopener=(Button)findViewById(R.id.btnpopupopen);
    btnpopupopener.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            LayoutInflater inflator= (LayoutInflater)PopupWindowActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout vi=(LinearLayout)inflator.inflate(R.layout.layout_popup_content, null);
            btnOpenActicivt= (Button)vi.findViewById(R.id.btnOpenActivity);
            tv_result=(TextView)vi.findViewById(R.id.tv_result);


            popwindow = new PopupWindow(vi, LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            popwindow.setContentView(vi);

            btnOpenActicivt.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent in= new Intent(PopupWindowActivity.this, SampleResultActivity.class);
                    startActivityForResult(in, POPRESULTcode);

                }
            });

            popwindow.showAsDropDown(btnpopupopener);

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    switch(resultCode){
    case POPRESULTcode:
            Log.i("Result"," Getting Message From Result Activity");

            tv_result.setText(data.getExtras().getString("resultstring"));
            popwindow.showAsDropDown(btnpopupopener);
        break;
    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.popup_window, menu);
    return true;
}


public class SampleResultActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample_result);

    Intent in= new Intent(getApplicationContext(), PopupWindowActivity.class);
    in.putExtra("resultstring", "Sending Sample String as Data");
    setResult(1, in);
    finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.sample_result, menu);
    return true;
}

}

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/btnpopupopen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="24dp"
    android:text="Open Popup" />

popupWindow布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/btnOpenActivity"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Go To next Activity Get Result back" />

<TextView
    android:id="@+id/tv_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="View Result Here"
    android:textAppearance="?android:attr/textAppearanceLarge" />