如何切换链接到动态创建的按钮的动态视图的可见性?

时间:2018-07-24 12:50:43

标签: java android textview

                            @Override
                        public void onResponse(String response) {
                            if (response != null) {
                                try {
                                    JSONArray jsonArray = new JSONArray(response);
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        final JSONObject jsonObject = jsonArray.getJSONObject(i);
                                        String title = jsonObject.getString("title");
                                        final String content = jsonObject.getString("content");
                                        final LinearLayout linearLayout = findViewById(R.id.lay);
                                        linearLayout.setOrientation(LinearLayout.VERTICAL);
                                        btn1.setVisibility(View.INVISIBLE);
                                        TextView txt = new TextView(MainActivity.this);
                                        final TextView txt1 = new TextView(MainActivity.this);
                                        final ImageView img = new ImageView(MainActivity.this);
                                        txt.setVisibility(View.VISIBLE);
                                        txt1.setVisibility(View.INVISIBLE);

txt.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));                                             txt1.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));                                             img.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));

                                        txt.setId(i);
                                        txt.setText(title + '\n');    
                                        txt.setGravity(1);
                                        img.setId(i);
                                        txt1.setId(i);
                                        txt.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                txt1.setVisibility(View.VISIBLE);
                                                txt1.setText(content);
                                                try {
                                                    Picasso.get().load(jsonObject.getString("icon")).into(img);
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        });
                                        txt.setWidth(900);
                                        txt1.setWidth(900);
                                        linearLayout.addView(img);
                                        linearLayout.addView(txt);
                                        linearLayout.addView(txt1);
                                    }

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

                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    textView.setText("That didn't work!");
                    Log.d("error", error.toString());
                }
            });
            queue.add(stringRequest);
        }

1 个答案:

答案 0 :(得分:0)

您只需要单击动态按钮的侦听器,然后将动态文本的可见性设置为txt.setVisibility(View.GONE);如果你希望它消失了, txt.setVisibility(View.VISIBLE);如果您希望它可见。如果您要切换它,只需创建一个布尔值即可跟踪状态。您需要这样的东西:

Boolean state = false;
if(state){
    txt.setVisibility(View.GONE);
    state = false;
else{
    txt.setVisibility(View.VISIBLE);
    state = true;
}