我在按钮下面动态创建一些按钮和一些相应的文本视图(按钮,textview,按钮,textview ...)我希望textView显示当我按下它上面的按钮并在我按下它时再次隐藏按钮。我为按钮和匹配的textview指定了相同的标签。 我希望它像这样工作:按下带有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。它不会让我为参数添加另一个视图,而我查看v,是按钮的视图。我该如何解决?
答案 0 :(得分:0)
在我看来,好像你的变量tv和myButton都是类成员。如果是这种情况,它们将在for循环的每一步被覆盖。这意味着一旦点击监听器触发它就会引用最近设置的tv和myButton值(即for循环的最后一步)。
将tv和myButton更改为方法范围内的变量,并将createButton()和createTextView()组合到单个方法中,或者让createTextView()返回创建的TextView并将其传递给createButton()
我对您的代码进行了一些更改:
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();
}
}
}