更改我的代码段上响应文本的颜色-在android中选择标记时

更改我的代码段上响应文本的颜色-在android中选择标记时,android,google-maps,google-maps-markers,Android,Google Maps,Google Maps Markers,我正在用谷歌地图标记在android上工作,请看下面的图片 我想根据响应改变Y&N的颜色,比如如果是Y,我想颜色应该是绿色,如果是N,那么颜色应该是红色 现在,Y和N都是红色,因为我编写了以下代码: <TextView android:id="@+id/response" android:layout_width="wrap_content" android:layout_height="wrap_content"

我正在用谷歌地图标记在android上工作,请看下面的图片

我想根据响应改变Y&N的颜色,比如如果是Y,我想颜色应该是绿色,如果是N,那么颜色应该是红色

现在,Y和N都是红色,因为我编写了以下代码:

<TextView
            android:id="@+id/response"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/response_lbl"
            android:textAllCaps="true"
            android:textColor="@color/event_response_s"
            android:textSize="18sp" />
marker.getSnippet()的值为Y或N,具体取决于用户将按下的按钮。我想用currentEvent.getButton_one()检查一下;,它给出了第一个按钮的值。 这里的问题是,我在声明currentEvent.getButton_one()的其他类中获得了正确的currentEvent.getButton_one()值,但在其他类(如CustomInfoWindow)中没有。最后一个条件应该这样检查:如果Y==Y--->绿色,否则为红色

请让我知道如果你需要更多的信息或知道我还尝试了什么

在Event.java中:

Intent i = new Intent(getApplicationContext(), MyActivity.class);  
i.putExtra("response1", currentEvent.getButton_one());
在MainActivity.java中:

String response_1 = getIntent().getStringExtra("response1");
现在,从MyActivity.java开始,我必须使用CustomInfoWinidow.java中的response_1进行比较:

if (marker.getSnippet().equals(response_1)) { 
    response.setTextColor(context.getResources().getColor(R.color.green)); 
} else  
   response.setTextColor(context.getResources().getColor(R.color.red));
}
条件:

    if (marker.getSnippet().equals("Y")){
            response.setTextColor(context.getResources().getColor(R.color.green));
        }
if (marker.getSnippet().equals("N")) {
            response.setTextColor(context.getResources().getColor(R.color.red));
        }
不对,一定是:

    if (marker.getSnippet().equals("Responded Y")){
            response.setTextColor(R.color.green);
        }
if (marker.getSnippet().equals("Responded N")) {
            response.setTextColor(R.color.red));
        }
并确保事件仅在单击标记时发生。
希望这将帮助您编辑:尝试将响应1传递到
CustomInfoWindow的构造函数中

public class CustomInfoWindow implements InfoWindowAdapter { 

public static final String DEVICE_ACTIVE = "Device Active"; 

private String response_1; //Add this as a member variable

//.......


public CustomInfoWindow(Context context, String response_1_custom) {
  this.response_1 = response_1_custom; //set the value here 
  this.context = context; 
  inflater = (LayoutInflater) LayoutInflater.from(context); 
} 

public View getInfoContents(Marker marker) { 

//........


//Now this should work:
try { 
if (marker.getSnippet().equals(response_1)) { 
response.setTextColor(context.getResources().getColor(R.color.green)); 
} else response.setTextColor(context.getResources().getColor(R.color.red)); 
}catch(Exception e){ 

} 
if (marker.getSnippet().equals(DEVICE_ACTIVE)) { 
response.setTextColor(context.getResources().getColor(R.color.red)); 
} 
response.setText(marker.getSnippet()); 
return view; 
} 
//.....................
然后在MainActivity.java中,在创建
CustomInfoWindow时传入
response\u 1

if (user_id.equalsIgnoreCase(userid)) { 
  map.setInfoWindowAdapter(new CustomInfoWindow(getBaseContext(), response_1)); //added response_1 to constructor parameter
  map.moveCamera(CameraUpdateFactory.newLatLngZoom(PERTH, 12)); 
} 
原始答复:

我使用了
InfoWindowAdapter
MarkerClickListener
的组合来实现它

基本上,您需要在
InfoWindowAdapter
中设置代码段的文本颜色,并在
MarkerClickListener
中设置
标记的颜色

      mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {

                Log.d("marker", "marker click");

                String s =  marker.getSnippet();
                Log.d("marker", "snippet: " + s);

                if (s.equals("Y")){
                    marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));

                }
                else if (s.equals("N")){
                   marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
                }

                return false;

            }

        });



        mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker arg0) {

                View v = getLayoutInflater().inflate(R.layout.customlayout, null);

                //set other fields.....

                //Snippet:
                TextView tSnippet = (TextView) v.findViewById(R.id.snippet);

                if (arg0.getSnippet().equals("N")){
                    tSnippet.setTextColor(Color.RED);

                }
                else if (arg0.getSnippet().equals("Y")){
                    tSnippet.setTextColor(Color.GREEN);
                }

                return v;

            }
        });
结果:


对于响应的单词,我有不同的XML
其中,info\u窗口为“responsed”提供灰色,getSnippet()方法返回什么?试着用syso或toast打印它。制作文本并检查它的内容,在我看来,当我使用
if(marker.getSnippet().equals(DEVICE_ACTIVE)){response.setTextColor(context.getResources().getColor(R.color.red))}时,使用它的条件或方式有问题
它会将活动设备的颜色更改为红色,或者如果我将其更改为绿色,则会将其更改为绿色。这意味着getSnippet()是正确的。好的,你能帮我发布整个类代码来帮助我理解整个情况吗?你希望标记颜色始终基于代码段值吗?绿色表示Y,红色表示N?标记颜色在这里是正确的绿色表示Y,红色表示N,我希望上面图像中红色的响应“Y”为绿色,当有“N”时,它应更改为红色根据您的回答,它将始终检查Y和N,但它可以是是或否、是或否、A或B任何内容。这就是为什么我要用currentEvent.getButton_one()检查if条件,它将给出管理员提到的任何选项。如果您想了解更多信息,请告诉我。@AtharvaPuranik啊,用这个例子让代码正常工作应该不会太难。只需比较一下
currentEvent.getButton_one()
,而不是我的简单示例中的硬编码值。正如我在currentEvent.getButton_one()的问题值中所提到的,声明它的位置(即MyActivity.java)是正确的,但当我尝试在CustomInfoWindow.java中使用它时,我没有得到正确的值。我无法访问CustomInfoWindow.java中的响应_1。如何执行?
      mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {

                Log.d("marker", "marker click");

                String s =  marker.getSnippet();
                Log.d("marker", "snippet: " + s);

                if (s.equals("Y")){
                    marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));

                }
                else if (s.equals("N")){
                   marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
                }

                return false;

            }

        });



        mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker arg0) {

                View v = getLayoutInflater().inflate(R.layout.customlayout, null);

                //set other fields.....

                //Snippet:
                TextView tSnippet = (TextView) v.findViewById(R.id.snippet);

                if (arg0.getSnippet().equals("N")){
                    tSnippet.setTextColor(Color.RED);

                }
                else if (arg0.getSnippet().equals("Y")){
                    tSnippet.setTextColor(Color.GREEN);
                }

                return v;

            }
        });