博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
按键——Button事件监听器
阅读量:6855 次
发布时间:2019-06-26

本文共 667 字,大约阅读时间需要 2 分钟。

hot3.png

button_1.setOnClickListener(new View.OnClickListener() {           

   @Override

   public void onClick(View v) {

      String button_1_content=button_1 .getText().toString();

           //获得按键的内容,读取的数据来自android:text              

     editText=(EditText)findViewById(R.id.editText);

  

     textContent=editText .getText().toString();

               

     text=textContent +button_1_content;

   /*

   *这里文本的输出内容必须是textContent+button_1_content

   *如果只把button_1_content赋值给text,

   *那么在按下按键1时会清除之前文本框中输入的内容,只显示一个数字1

   **/

  editText.setText(text );  

  editText.setSelection(text .length());

   /*

   *setSelection()的作用是定位光标

   *该句话的意思是光标的位置为当前字符串长度的后一个位置

   *注意:不能将光标的位置定位在当前长度的后面,即          

   *editText.setSelection(text.length()+1),这样会闪退

   **/

转载于:https://my.oschina.net/u/2312175/blog/635226

你可能感兴趣的文章