How to get the text value of a button and load it into another button in Android

时间:2017-08-13 13:46:05

标签: android

I am creating a word game application , I have created a grid view consisting of buttons. When I click a button in the grid view a popup windows opens which contains all the English alphabets.Now when I click any letter in my pop up window , I want that letter to appear in my grid that is the string in my pop window's button must appear in my grid view's button. , how do I do it?

This is my button's code:

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

My popup window's code:

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

Screenshots of the app

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.

If I run this , the app is getting stopped.

1 个答案:

答案 0 :(得分:0)

  1. It might help us if you would share the exact exception that is thrown by your application.
  2. If I unterstand you correctly, then b1 is a button contained in your activity_popup- layout. So you might want to try b1 = (Button) container.findViewById(R.id.b1) instead. If thats not working, post your exception!