[英]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.