【问题标题】:Show TextView corresponding to button tag显示按钮标签对应的TextView
【发布时间】:2013-05-07 09:39:31
【问题描述】:

我正在按钮下方动态创建一些按钮和一些相应的文本视图(按钮、文本视图、按钮、文本视图 ...)按钮。我为按钮和匹配的文本视图提供了相同的标签。 我希望它像这样工作:用 tag1 按下按钮,用 tag1 显示 textview,再次按下按钮,它会隐藏它。 现在它只在我按下任何按钮时显示和隐藏最后创建的文本视图。 更新代码:

    private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {
    return readJSONFeed(urls[0]);
    }

    protected void onPostExecute(String result) {
        Button button1;
        TextView tv;
        try {
            JSONArray jsonArray = new JSONArray(result);
            Log.i("JSON", "Number of surveys in feed: " +
            jsonArray.length());

            LinearLayout news = (LinearLayout)findViewById(id.hello);
            //---print out the content of the json feed---
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                button1 = new Button(getApplicationContext());
                button1.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                button1.setId(i);
                tv = new TextView(getApplicationContext());
                tv.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                tv.setId(i);
                button1.setText(jsonObject.getString("title"));
                tv.setText(jsonObject.getString("text"));


                news.addView(button1);
                news.addView(tv);

                button1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        v.isPressed();
                            if(v.getVisibility() == View.GONE) {
                                v.setVisibility(View.VISIBLE);  
                        } else {
                            v.setVisibility(View.GONE);
                        }
                    }
                });



                Toast.makeText(getBaseContext(), jsonObject.getString("title") +
                        " - " + jsonObject.getString("text"),
                        Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
            e.printStackTrace();
            }
        }
    }

现在的问题是,如何在 onClick 方法中调用变量 tv。它不会让我向参数添加另一个视图,而 i View v 是按钮的视图。我该如何解决?

【问题讨论】:

    标签: java android


    【解决方案1】:

    在我看来,变量 tv 和 myButton 是类成员。如果是这种情况,它们将在 for 循环的每一步被覆盖。这意味着一旦点击监听器触发,它将引用最近为 tv 和 myButton 设置的值(即 for 循环的最后一步)。

    将 tv 和 myButton 更改为方法范围内的变量,然后将 createButton() 和 createTextView() 组合成一个方法,或者让 createTextView() 返回创建的 TextView 并将其传递给 createButton()

    我对您的代码进行了一些更改:

    • 在 for 循环中移动了 Button 和 TextView 声明
    • 将 onClick 侦听器更改为使用本地 TextView 引用。

    PS 我不确定你为什么在 onClick() 中调用 v.isPressed()。它什么都不做。

    protected void onPostExecute(String result) {
        try {
            JSONArray jsonArray = new JSONArray(result);
            Log.i("JSON", "Number of surveys in feed: " +
            jsonArray.length());
    
            LinearLayout news = (LinearLayout)findViewById(id.hello);
            //---print out the content of the json feed---
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
    
                final Button button = new Button(getApplicationContext());
                button.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                button.setId(i);
                button.setText(jsonObject.getString("title"));
    
                final TextView tv = new TextView(getApplicationContext());
                tv.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                tv.setId(i);
                tv.setText(jsonObject.getString("text"));
    
                news.addView(button);
                news.addView(tv);
    
                button.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv.setVisibility(tv.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
                    }
                });
    
                Toast.makeText(getBaseContext(), jsonObject.getString("title") +
                        " - " + jsonObject.getString("text"),
                        Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
            e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • 好的,我现在已经将 tv 和 myButton 添加到了作用域中,并结合了这两个 create 方法。现在的问题是,如何让 onClick 方法包含 textview。当我只被允许在我的 onClick 中拥有一个视图时。
    猜你喜欢
    • 1970-01-01
    • 2011-10-10
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    相关资源
    最近更新 更多