-
21-12-2019 - |
题
嗨,我想知道为什么更新按钮的文本在我的应用程序中表现不同。
我调用一个获取按钮应该更新的信息的片段,我从片段中执行接口回调以更新活动更新按钮文本的全局变量。
问题是按钮所在的活动不会刷新按钮以显示新文本,但如果我通过片段执行它,则无效按钮不起作用或强制刷新,如果我通过活动执行。
以下是当您按下按钮时调用的活动,然后调用显示列表的片段,当您单击选项时,按钮应将其文本更改为您选择的任何内容:
public void onClick_testSite(View view)
{
// create the fragment
SelectChartTypeDialogFragment chartType = new SelectChartTypeDialogFragment();
// register you as a delegate for the callback
chartType.delegate = this;
// show the list
chartType.show(getFragmentManager(), "WEE");
// fetch the button and set the text ** DOESNT WORK **
Button p1_button = (Button)findViewById(R.id.btn_pickChart);
p1_button.setText(response);
p1_button.invalidate();
}
@Override
public void processFinish(String response)
{
this.response = response;
}
这里是处理对话框的片段的一部分:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// get the list from a enum
final List<ChartTypes> chartList = Arrays.asList(ChartTypes.values());
// The array containing the different choices
ArrayAdapter<ChartTypes> adapter = new ArrayAdapter<ChartTypes>(
getActivity(), android.R.layout.simple_list_item_1, chartList);
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// The dialog setting
builder.setTitle(R.string.pick_chart);
builder.setAdapter(adapter, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int position)
{
// get the name of the enum based on the position, what one
// clicked on the dialog
String strName = chartList.get(position).name();
// this sets the text in the activitys global variable
delegate.processFinish(strName);
// set the text in the fragment instead
//changeBtnText(strName);
//dialog.dismiss();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
public void changeBtnText(String newBtnxt)
{
Button button = (Button)getActivity().findViewById(R.id.btn_pickChart);
button.setText(newBtnxt);
}
我的问题是,为什么它通过片段而不是通过活动更新gui上的文本(运行应用程序时),即方法p1_button。setText(response);?
编辑答案正如Raghunandan所解释的那样:问题是我不明白 onClick_testSite(View view)
完成,即使你没有点击对话框上的任何东西,我以为它等待的函数调用 chartType.show()
它返回,然后继续到函数的末尾。
解决方案
你需要初始化 public AsyncResponse delegate = null;
delegate =(AsyncResponse) getActivity();
我相信这是实现的方法
@Override
public void processFinish(String response)
{
Log.i(".........",response); // check if the response is logged
// if you get the response your button text will be changed
// else you need to look at why the response is not logged.
p1_button.setText(response);
}
声明 Button p1_button
作为实例变量(例如在onCreate之前)。
初始化此
p1_button = (Button)findViewById(R.id.btn_pickChart); // in onCreate
可能按钮甚至在收到响应之前就被刷新了 processFinish
.然后初始化响应变量。因此,在下次单击按钮时设置按钮文本。
您可以在onCreate之前声明按钮并在 processFinish
而不是上面的hwon。
其他提示
该 加工,加工 将值保存到 回应.但它不适用于TextView与 btn_pickChart 身份证。
所以,你只需要保存Textview的实例活动:
private Button mP1Button;
protected void onCreate(Bundle savedInstanceState) {
...
Button mP1Button = (Button) findViewById(R.id.btn_pickChart);
...
}
并应用更改后的值时 加工,加工 被称为:
public void processFinish(String response) {
this.response = response;
// The value will be setup to the view element.
mP1Button.setText(response);
}
最好不要使用委托作为字段。您可以使用getActivity()或getTargetFragment()来检查Instanceof AsyncResponse并调用processFinish方法。
AsyncResponse asyncResponse = null;
if(getActivity() instanceof AsyncResponse) {
asyncResponse = (AsyncResponse) getActivity();
} else if (getTargetFragment() instanceof AsyncResponse){
asyncResponse = (AsyncResponse) getTargetFragment();
}
if(asyncResponse != null){
asyncResponse.processFinish(strName);
}
顺便说一句,不需要打电话给 作废 后的方法 n.移民,移民 因为它已经从这里调用。