如何将textview值分配给按钮?

时间:2016-05-06 09:19:32

标签: javascript android

我有一个函数实际上将名称设置到文本视图中,我有一个按钮监听器,它实际上是在oncreate方法中定义的。所有名称都有一个唯一的对象id。我需要将所选名称的对象id添加到按钮的onclick函数中,并且此对象id通过bundle extras将intent传递给下一个activity。该函数在oncreate方法之外定义。所有名称和相应的id正在通过网络服务电话。

设置文本视图的功能是

AudioManager.OnAudioFocusChangeListener audiofocusListener =
      new AudioManager.OnAudioFocusChangeListener() {
        @Override public void onAudioFocusChange(int focusChange) {
          switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN: {
            }
            break;
            case AudioManager.AUDIOFOCUS_LOSS: {
            }
            break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: {
            }
            break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
            }
            break;
          }
        }
      };

按钮的onclick功能是

   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;
            }
        }

    });
}

1 个答案:

答案 0 :(得分:0)

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

1。通过传递Slecteditem1字符串调用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);