標籤:
Button類提供了控制按鈕功能,Button類屬於Android.Wiget包並且繼承Android.widget.TextView類,button類提供了操縱控制按鈕的方法和屬性。
常用的方法和功能:
| 方法 |
功能描述 |
| Button |
Button類的構造方法 |
| setOnKeyListener |
設定按鍵監聽 |
| onKeyLongPress |
當使用者保持按鍵時,該方法被調用 |
| onKeyUp |
當使用者按鍵彈起後,該方法被調用 |
| onKeyMultiple |
當使用者多次按鍵時,該方法被調用 |
| onKeyDown |
當使用者按鍵時,該方法被調用 |
Button標籤的屬性(在xml中修改和設定)
| 屬性名稱 |
描述 |
| android:layout_height |
設定控制項高度,可選值(fill_parent/warp_contect/px值) |
| android:layout_weight |
設定控制項寬度,可選值(fill_parent/warp_contect/px值) |
| android:text |
設定控制項名稱,可選值(任一字元串) |
| android:layout_gravity |
設定控制項在布局中的位置,可選值(top/bottom/left/right/center_verticle/fill_verticle/fill_horizontal/center/fill/cilp_verticle) |
| android:layout_weight |
設定控制項在布局中的比重,可選值(任一數字) |
| android:textColor |
設定文字顏色,可選值(任意顏色值,如0xFFFFFFFF) |
| android:bufferType |
設定取得的文本類別,可選值(normal/spannable/editable) |
| android:hint |
設定文本空時顯示的字元,可選值(任一字元串如“請點擊按鈕”) |
具體例子說明:
1 ackage com.example.hello2; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.widget.TextView; 8 import android.view.View; 9 import android.widget.Button;10 11 public class MainActivity extends ActionBarActivity {12 private TextView myTextView;13 private Button mButton1;14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.activity_main);18 19 mButton1=(Button) findViewById(R.id.myButton1);20 myTextView=(TextView) findViewById(R.id.myTextView);21 22 mButton1.setOnClickListener(new Button.OnClickListener()23 {24 @Override25 public void onClick(View v)26 {27 myTextView.setText("Hi,Everyone!!");28 }29 });30 }31 32 @Override33 public boolean onCreateOptionsMenu(Menu menu) {34 // Inflate the menu; this adds items to the action bar if it is present.35 getMenuInflater().inflate(R.menu.main, menu);36 return true;37 }38 39 @Override40 public boolean onOptionsItemSelected(MenuItem item) {41 // Handle action bar item clicks here. The action bar will42 // automatically handle clicks on the Home/Up button, so long43 // as you specify a parent activity in AndroidManifest.xml.44 int id = item.getItemId();45 if (id == R.id.action_settings) {46 return true;47 }48 return super.onOptionsItemSelected(item);49 }50 }
在java主程式中加入上述代碼。
在xml中也要做相應修改,這裡需要提一下,layout中提供了可視化布局,可以通過手動調整位置(代碼會自動在xml中修改)。
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.hello2.MainActivity" >10 11 <TextView12 android:id="@+id/myTextView"13 android:layout_width="wrap_content"14 android:layout_height="wrap_content"15 android:text="@string/hello" />16 17 <Button18 android:id="@+id/myButton1"19 android:layout_width="wrap_content"20 android:layout_height="wrap_content"21 android:layout_alignLeft="@+id/myTextView"22 android:layout_below="@+id/myTextView"23 android:layout_marginTop="14dp"24 android:text="點擊" />25 26 </RelativeLayout>
添加Button的屬性設定
最終效果:
點擊後:
4th-安卓UI操作-按鈕