How to create counter on button?

后端 未结 3 1069
你的背包
你的背包 2021-01-28 15:44

I want to create button with text = \"SomeText\" on center of this button and \"0\" on right part of button. Where \"0\" is the Counter and when I click this button Counter++ i

3条回答
  •  梦毁少年i
    2021-01-28 16:44

    Try this way,hope this will help you to solve your problem.

    main.xml

    
        

    strings.xml

    Some Text  %1d
    

    MyActivity.java

    public class MyActivity extends Activity{
    
        private Button btnCounter;
        private int count;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            btnCounter = (Button) findViewById(R.id.btnCounter);
    
            btnCounter.setText(String.format(getString(R.string.button_text),count));
    
            btnCounter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    count++;
                    btnCounter.setText(String.format(getString(R.string.button_text),count));
                }
            });
        }
    
    }
    

提交回复
热议问题