【问题标题】:How to assign the textview value in to the button?如何将 textview 值分配给按钮?
【发布时间】:2016-05-06 09:19:32
【问题描述】:

我有一个函数,它实际上将名称设置到文本视图中,并且我有一个按钮侦听器,它实际上是在 oncreate 方法中定义的。所有名称都有一个唯一的对象 id。我需要将所选名称的对象 ID 获取到按钮的 onclick 函数中,并且该对象 ID 通过捆绑附加功能将意图传递给下一个活动。该函数在 oncreate 方法之外定义。所有名称和相应的 ID正在通过网络服务调用。

设置文本视图的功能是

   public  void setMarker(){

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {

            SaloonDetails = "";
            // Take some action here
            String snippet = marker.getSnippet();
            objectId = snippet;

            if (selectedMarker != null && !("currentLocation".equals(snippet))) {
                selectedMarker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.map_red));
            }

            selectedMarker = marker;
            if (!("currentLocation".equals(snippet)))
                marker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.map_blue));

            Log.i("TAG", "snippet::::" + snippet);

            if ("currentLocation".equals(snippet)) {
                selectedLayout.setVisibility(View.GONE);
                //button_bookmark.setVisibility(View.GONE);
                return false;
            } else {

                selectedLayout.setVisibility(View.VISIBLE);

                TextView nameView = ((TextView) selectedLayout
                        .findViewById(R.id.name_txt_id));

                TextView addressView = ((TextView) selectedLayout
                        .findViewById(R.id.address_txt_id));

                Button button = ((Button) selectedLayout
                        .findViewById(R.id.selec_button_id));

                try {
                    JSONObject jSalonData = (JSONObject)SalonData.get(snippet);

                    nameView.setText(jSalonData.getString("SalonName"));

                   // button.setText(jSalonData.getString("SalonName"));
                    String  Slecteditem1= nameView.getText().toString();

                    Log.i("Sltitem olemapsAivity",Slecteditem1);

                } catch (Exception e) {
                    e.printStackTrace();
                }


                try {
                    JSONObject jSalonData = (JSONObject)SalonData.get(snippet);
                    Log.i("jSalonData",jSalonData.toString());

                    City = jSalonData.optString("City");
                    SaloonDetails = SaloonDetails + City;
                    Zipcode = jSalonData.optString("Zipcode");
                    SaloonDetails = SaloonDetails  +" "+ Zipcode;
                    HouseNumber = jSalonData.optString("HouseNumber");
                    SaloonDetails = SaloonDetails  +" "+ HouseNumber;
                    Street = jSalonData.optString("Street");
                    SaloonDetails = SaloonDetails +" "+ Street;
                    Log.i("SaloonDetails", SaloonDetails);
                    addressView.setText(SaloonDetails);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
        }

    });
}

按钮的onclick功能是

       Button button = ((Button)         

    selectedLayout.findViewById(R.id.selec_button_id));

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        // On click function
        public void onClick(View view) {

           Bundle extras = new Bundle();
            Button b = (Button)view;
            String buttonText = b.getText().toString();
            Log.i("buttonText",buttonText);
            //String Slecteditem = hm.get(buttonText);

            //Log.i("objectidddddd",Slecteditem);

            Intent intent = new Intent(view.getContext(),
                    Salon_Detail.class);
            //extras.putString("Slecteditem", Slecteditem);
            startActivity(intent);
        }
    });

【问题讨论】:

标签: javascript android


【解决方案1】:

使用Button的setTag/getTag方法根据点击的Button获取文本为:

1.通过传递Slecteditem1String调用setTag方法:

  String  Slecteditem1= nameView.getText().toString();
  button.setTag(Slecteditem1);

2.onClick 方法中,使用getTag() 从Button 中获取选定的文本。

 ...
 Bundle extras = new Bundle();
 extras.putString("Slecteditem", view.getTag().toString());
 startActivity(intent);

【讨论】:

  • 如何获取所选名称对应的id。它仅在 setmarker 函数中可用。如何通过两者?
  • @Maria:Maria 总是改变问题的焦点。如果您想要 setmarker 方法中的其他项目,那么不要在 setTag 中使用 Slecteditem1 button.setTag(jSalonData.toString()); 并在 onClick 方法中解析 jsonobject 以获取其他值
猜你喜欢
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
相关资源
最近更新 更多