第十三章:經常使用控件上

1、廢話

  今天將介紹android系統爲咱們提供的經常使用控件中的TextViewButtonEditTextRadioButtonCheckBoxToggleButtonRatingButton七個控件的聲明和事件響應。html

2、正文

  一、 TextViewjava

    相似ASP.NET中的Label控件,只讀顯示控件,可經過getText()獲取其android:text屬性、setText()設置其android:text屬性。在res/layout/main.xml的LinearLayout節中添加以下代碼來聲明TextView。android

 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/myTextView" />

在java代碼中能夠經過下列代碼取得該控件。ide

//取得該控件
TextView myTextView =(TextView)findViewById(R.id.myTextView);

二、 Buttonthis

 按鈕控件,用戶經過該控件來提交本身的請求,是用戶與應用程序交互的最經常使用控件之一。spa

  在res/layout/main.xml中聲明控件3d

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/myButton" />

那麼如何響應用戶的動做呢?code

//響應Button的Click事件
        
        myButton = (Button) findViewById(R.id.myButton);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(Android_Control_DemoActivity.this,
                        "click Button", Toast.LENGTH_SHORT).show();
            }
        });

三、 EditTextxml

    Android系統提供給用戶輸入的文本框,如ASP.NET中TextBox。htm

    在res/layout/main.xml中添加EditText標籤。

<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/myEditText" />

讓咱們的EditText一個回車動做,請看下面的代碼,代碼裏演示瞭如何取得EditText的值以及如何爲TextView賦值。

//操做EditText控件,取值以及響應事件
        myEditText = (EditText)findViewById(R.id.myEditText);
        myEditText.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // 響應用戶的回車鍵動做,將EditText中值顯示到TextView中
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&(keyCode == KeyEvent.KEYCODE_ENTER)) {
                      myTextView.setText(myEditText.getText());
                      return true;
                    }
                    return false;
            }
        });

四、  RadioButton

    單選按鈕,放在一個RadioGroup中,在這個group中只能有一個選項可以被選中,好比你選擇性別時,只能選擇一個性別。

    在layout/main.xml中聲明控件。

<RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:text="Red" android:layout_height="wrap_content" android:checked="true"></RadioButton>
        <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:text="Blue" android:layout_height="wrap_content"></RadioButton>
        <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:text="Green" android:layout_height="wrap_content"></RadioButton>
    </RadioGroup>

響應RadioButton

myRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(radio_1.getId()==checkedId)
                {
                    Toast.makeText(Android_Control_DemoActivity.this, radio_1.getText(), Toast.LENGTH_SHORT).show();
                }
                if (radio_2.getId()==checkedId)
                {
                    Toast.makeText(Android_Control_DemoActivity.this, radio_2.getText(), Toast.LENGTH_SHORT).show();
                }
                if(radio_3.getId()==checkedId)
                {
                    Toast.makeText(Android_Control_DemoActivity.this, radio_3.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });

五、 CheckBox

    複選按鈕。在應用程序中若是讓用戶選擇本身的喜愛時,由於用戶的喜愛不可能只有一個,因此這時你必須使用複選按鈕,容許用戶多選。

    在res/layout/main.xml中聲明控件。

<CheckBox android:text="CheckBox" android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>

下面來響應用戶的選擇操做,一般狀況下,複選框選擇後是有一個按鈕來統一提交的,可是咱們在例子中只演示如何響應CheckBox的Change事件。

//爲myChecBox控件綁定響應事件
        myCheckBox = (CheckBox)findViewById(R.id.checkBox1);
        myCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    Toast.makeText(Android_Control_DemoActivity.this, myCheckBox.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });

六、 Toggle Button

    若是有用過android系統的人都知道,在開啓和關閉WIFI的時候,有一個按鈕,當你開啓WIFI時按鈕顯示爲On,當你關閉WIFI時按鈕顯示爲OFF,這個按鈕只有這兩種狀態。這個按鈕就是ToggleButton。

    在res/layout/main.xml中定義控件

<ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="On" android:textOff="Off"></ToggleButton>

響應Toggle Button的Checked事件

//響應myToggleButton的Checked事件
        myToggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
        myToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    Toast.makeText(Android_Control_DemoActivity.this, myToggleButton.isChecked()+"", Toast.LENGTH_SHORT).show();
                }
            }
        });

七、 Rating Bar

    評分條。能夠根據咱們的須要來定義他的增加速率和最大值。

    在res/layout/main.xml中聲明控件

<RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="1.0"></RatingBar>

android:numStarts——打分條中五角星的個數

android:stepSize——也就是你點擊一次,會選中一個星星的多少,若是爲1.0,那點擊一次就是一顆星,若是設置爲0.5那麼就是半顆星。

//響應nyRatingBar的RatingBarChange事件
        myRatingBar =(RatingBar)findViewById(R.id.ratingBar1);
        myRatingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                Toast.makeText(Android_Control_DemoActivity.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
            }
        });

 八、最後來個效果圖

相關文章
相關標籤/搜索