有关Botton的用法(二)
生活随笔
收集整理的這篇文章主要介紹了
有关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" />?
轉載于:https://www.cnblogs.com/turtle920/p/4860740.html
總結
以上是生活随笔為你收集整理的有关Botton的用法(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到买新车啥意思
- 下一篇: TCP的流模式与UDP的报文模式对比