zoukankan      html  css  js  c++  java
  • 有关Botton的用法(二)

    关于设置listener监听onClicked事件的步骤分析

    Steps:

    1.tell android you are interested in listening to a button click

    2.bring your xml button inside java

    3.tell your java button whose responding when it's clicked

    4.what should happen when button is clicked

    1     <Button
    2         android:layout_width="wrap_content"
    3         android:layout_height="wrap_content"
    4         android:text="Button1"
    5         android:id="@+id/button1"
    6         android:layout_below="@+id/textView"
    7         android:layout_alignParentLeft="true"
    8         android:layout_alignParentStart="true" />
     1 public class MainActivity extends Activity implements OnClickListener {//1.tell android you are interested in listening to a button click
     2 
     3     Button button;
     4     @Override
     5     protected void onCreate(Bundle savedInstanceState) {
     6         super.onCreate(savedInstanceState);
     7         setContentView(R.layout.activity_main);
     8         button = (Button)findViewById(R.id.button1);//2.bring your xml button inside java
     9         button.setOnClickListener(this);//3.tell your java button whose responding when it's clicked
    10     }
    11 
    12     @Override//4.what should happen when button is clicked
    13     public void onClick(View v) {
    14         Log.e("MainActivity","Clicked1");
    15     }
    16 
    17 }

     监听多个button:

     1         button1 = (Button)findViewById(R.id.button1);
     2         button1.setOnClickListener(this);
     3         button2 = (Button)findViewById(R.id.button2);
     4         button2.setOnClickListener(this);
     5         button3 = (Button)findViewById(R.id.button3);
     6         button3.setOnClickListener(this);
     7 
     8 @Override
     9     public void onClick(View view) {
    10         if(view.getId()==R.id.button1){//复制
    11             TextView textView = (TextView)findViewById(R.id.textView);
    12             TextView textView1 = (TextView)findViewById(R.id.editText);
    13             textView.setText(""+textView1.getText());
    14 
    15         }
    16         if(view.getId()==R.id.button2){//锁定
    17             TextView textView = (TextView)findViewById(R.id.editText);
    18             //textView.setFocusable(false);
    19             //textView.setFocusableInTouchMode(false);
    20             //textView.setEnabled(false);
    21             keyListener=textView.getKeyListener();
    22             textView.setKeyListener(null);
    23             InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);//这两行是收起软键盘
    24             imm.hideSoftInputFromWindow(textView.getWindowToken(),0);
    25 
    26         }
    27         if(view.getId()==R.id.button3){//编辑
    28             TextView textView = (TextView)findViewById(R.id.editText);
    29             textView.setKeyListener(keyListener);
    30             textView.setFocusable(true);
    31             textView.setFocusableInTouchMode(true);
    32             textView.requestFocus();
    33 
    34         }
    35   }

    布局如下:

     1 <TextView
     2           android:text="@string/hello_world"
     3           android:layout_width="wrap_content"
     4           android:layout_height="wrap_content"
     5           android:id="@+id/textView" />
     6 
     7       <Button
     8           android:layout_width="wrap_content"
     9           android:layout_height="wrap_content"
    10           android:text="复制"
    11           android:id="@+id/button1"
    12           android:layout_alignParentLeft="true"
    13           android:layout_alignParentStart="true" />
    14 
    15       <Button
    16           android:layout_width="wrap_content"
    17           android:layout_height="wrap_content"
    18           android:text="锁定"
    19           android:id="@+id/button2"
    20           android:layout_alignParentLeft="true"
    21           android:layout_alignParentStart="true" />
    22 
    23       <Button
    24           android:layout_width="wrap_content"
    25           android:layout_height="wrap_content"
    26           android:text="编辑"
    27           android:id="@+id/button3"
    28           android:layout_centerHorizontal="true" />
  • 相关阅读:
    酒美网宣布获8000万风险投资 年底销售额达1.5亿元
    [置顶]做足这5点,老板立马会对你另眼相看
    企鹅快跑——腾讯敏捷历程揭秘
    Gmail Mobile 不可忽视的七个细节
    网上爆出ATM取款机存漏洞 黑客可获最高权限
    中国的土壤真的不适合软件生长!
    淘宝商城开放的B2C平台战略
    不是GC打酱油,是人打酱油
    腾讯离职元老的内部邮件:马化腾的趣事
    JAVA课程设计个人博客链接
  • 原文地址:https://www.cnblogs.com/turtle920/p/4860740.html
Copyright © 2011-2022 走看看