更新按钮上的文本

时间:2014-01-09 08:54:29

标签: android button android-fragments dialogfragment

您好我想知道为什么更新按钮文本在我的应用中表现不同。

我调用一个片段来获取按钮应该更新的信息,我从片段进行接口回调以更新活动更新按钮文本的全局变量。

问题是按钮所在的活动不会刷新按钮以显示新文本,但是如果我通过它运行的片段执行此操作,则无效按钮不起作用或强制刷新如果我这样做通过活动。

这是当您按下按钮然后调用显示列表的片段时调用的活动,当您单击某个选项时,按钮应将其文本更改为您选择的任何内容:

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()的函数调用它返回然后继续到最后功能。

2 个答案:

答案 0 :(得分:1)

您需要初始化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中更新它。

答案 1 :(得分:1)

processFinish 将值保存到响应。但它不适用于带有 btn_pickChart id的TextView。

因此,您只需要保存TextView for Activity的实例:

private Button mP1Button;

protected void onCreate(Bundle savedInstanceState) {
...
    Button mP1Button = (Button) findViewById(R.id.btn_pickChart);
...
}

在调用 processFinish 时应用更改后的值:

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

顺便说一下,无需在 setText 之后调用无效方法,因为它已经从此处调用。