【问题标题】:How to get the text value of a button and load it into another button in Android如何获取按钮的文本值并将其加载到Android中的另一个按钮中
【发布时间】:2017-08-14 05:22:17
【问题描述】:

我正在创建一个文字游戏应用程序,我创建了一个由按钮组成的网格视图。当我单击网格视图中的按钮时,会打开一个弹出窗口,其中包含所有英文字母。现在,当我单击弹出窗口中的任何字母时,我希望该字母出现在我的网格中,即弹出窗口按钮中的字符串必须出现在我的网格视图的按钮中。 ,我该怎么做?

这是我的按钮代码:

button1.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {

            layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            View container = layoutInflater.inflate(R.layout.activity_popup,null);
            popupWindow = new PopupWindow(container,800,1100,true);
            popupWindow.showAtLocation(constraintLayout, Gravity.NO_GRAVITY,150,400);
            b1=(Button) findViewById(R.id.b1);
            String s1 = b1.getText().toString();
            button1.setText(s1);
            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionevent){
                    popupWindow.dismiss();
                    return true;
                }
            });
        }
    });

我的弹窗代码:

b1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String s1 = "A";

                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }
        });

应用截图

This is my grid layout where the user must enter the letters

When I click any button on the grid this is the pop up window which is displayed.

如果我运行它,应用程序将停止。

【问题讨论】:

  • 添加你的错误日志
  • 添加你的 logcat。
  • 我是Android开发的新手,所以请详细解释一下。

标签: android


【解决方案1】:
  1. 如果您愿意分享您的应用程序引发的确切异常,这可能会对我们有所帮助。
  2. 如果我理解正确,那么b1 是包含在activity_popup- 布局中的按钮。所以你可能想试试b1 = (Button) container.findViewById(R.id.b1)。如果这不起作用,请发布您的例外情况!

【讨论】:

  • 您可以在 Android Studio 中监控应用的输出。左下角应该是一个按钮“Android Monitor”。你可以在那里查看所有日志,试试看。
猜你喜欢
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 2019-07-15
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
相关资源
最近更新 更多