android - 更新按钮上的文本

标签 android button android-fragments dialogfragment

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

我调用一个 fragment 来获取按钮应更新的信息,我从 fragment 中执行接口(interface)回调以更新 Activity 更新按钮文本的全局变量。

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

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

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

这是处理对话框的 fragment 的一部分:

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

我的问题是,为什么它通过 fragment 而不是通过 Activity 更新 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 那样。

关于android - 更新按钮上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21015532/

相关文章:

当设备的方向改变时,带有 fragment 的 android Activity 退出

Android:扩充布局并将其写入 PDF 会产生空白 PDF

html - TYPO3 6.2 RTE 无法添加按键

java - 如何通过 API 调用填充 Android 中的 AutoCompleteTextView?

javascript - 禁用按钮 (F12)

java - 按钮可能产生空指针异常(Android Studio)

android - CoordinatorLayout 内的 Viewpager 不滚动折叠工具栏

android - 如何从 Activity 的 fragment 中调用方法?

Android Studio Gradle 读取 ZIP 文件时出错

java - 用户如何通过点击 google maps android 添加标记?