有关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: />
 6 
 7       <Button
 8           android:layout_width="wrap_content"
 9           android:layout_height="wrap_content"
10           android:text="复制"
11           android:
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:
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:
28           android:layout_centerHorizontal="true" />