android 获取弹窗的值,从弹出窗口调用的Activity 返回一个值_popupwindow_开发99编程知识库...

你应该创建具有有限宽度和高度的透明 Activity,而不是弹出对话框。 这样它就像对话框,然后你可以打开另一个 Activity 并使用 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;

}

}

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world"/>

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布局

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

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"/>

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"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值