[英]How to display text from one activity to another activity layout?
我正在开发一个谷歌地图项目,用户可以长按地图上的特定点来放置标记。 然后在点击该标记时会弹出一个自定义对话框窗口。 所有这些都很好用,但我需要做的是在自定义对话框窗口的文本视图中显示该标记的纬度和经度。
所以对于自定义对话框,我有以下内容:
public class CustDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_dialogbox);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView text = (TextView) dialog.findViewById(R.id.txt_location);
text.setText(msg);
Button dialogBtn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
dialogBtn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button dialogBtn_okay = (Button) dialog.findViewById(R.id.btn_okay);
dialogBtn_okay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {,Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
dialog.show();
}
在标记上单击事件我有这行代码来显示自定义对话框窗口:
ViewDialog alert = new ViewDialog();
alert.showDialog(MapsActivity.this, "New Marker Point");
为了获取标记的当前位置,我将这些行添加到标记单击事件中:
LatLng latLng = marker.getPosition();
当我尝试添加文本视图以显示文本时:
TextView txtView = findViewById(R.id.txt_location);
txtView.setText("Latitude:" + latLng.latitude);
所以完整的标记点击事件将是:
mMap.setOnMarkerClickListener((new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(@NonNull Marker marker) {
LatLng latLng = marker.getPosition();
ViewDialog alert = new ViewDialog();
alert.showDialog(MapsActivity.this, "New Marker Point");
TextView txtView = findViewById(R.id.txt_position);
txtView.setText("Latitude:" + latLng.latitude);
return false;
}
}));
然后我收到以下错误:
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
有没有办法将文本从 MapsActivity 类传递到 CustDialog 类?
谢谢
您必须首先拥有调用对话框的纬度和经度才能通过对话框,在对话之后,您可以轻松有效地使用纬度和经度,在这种情况下,您没有问题,或者您可以拥有。 随时随地使用它。
1 - 您的自定义对话框
public class ViewDialog extends Dialog {
Context _context;
TextView txtView ;
public ViewDialog(Context context) {
super(context, R.style.DialogStyle);
this._context=context;
}
public void showDialog(String marketName, LatLng latLng) {
requestWindowFeature(Window.FEATURE_NO_TITLE); // optional code
setContentView(R.layout.modal_view_dialog);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); // optional code
txtView=findViewById(R.id.txt_position); // your textView witch in your dialog
txtView.setText(latLng!=null ? " Latitude:"+latLng.latitude+" Longitude:"+latLng.longitude : "latLng is failed !");
}
/// another method of dialog with you want to use
}
方法中的 2 调用对话框
mMap.setOnMarkerClickListener((new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(@NonNull Marker marker) {
LatLng latLng = marker.getPosition();
ViewDialog alert = new ViewDialog(MapsActivity.this);
alert.showDialog("New Marker Point",latLng);
alert.show();
return false;
}
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.