android基礎入門控制項詳解(4)

來源:互聯網
上載者:User

一.Button、TextView、EditText、RadioButton、RadioGroup、CheckBox綜合使用:

布局檔案:

activity_main.xml:

                                                                


strings.xml:

    HelloWorld    Settings    詞語拼接    提交    名字    星星    月亮    太陽    天    地    海

MainActivity.java:

package com.example.helloworld;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity {    //用來輸入姓名private EditText name;//建立RadioGroup對象private RadioGroup rg;//建立三個RadioButton對象private RadioButton sun,moon,stars;//建立三個CheckBox對象private CheckBox day,theearth,sea;//建立提交按鈕private Button submit;//用於顯示的文本String text="";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//findViewById()方法獲得布局檔案中的控制項,通過Id擷取。name =(EditText)findViewById(R.id.name);rg = (RadioGroup)findViewById(R.id.RadioGroup);sun = (RadioButton)findViewById(R.id.sun);moon = (RadioButton)findViewById(R.id.moon);stars = (RadioButton)findViewById(R.id.stars);day = (CheckBox)findViewById(R.id.day);theearth = (CheckBox)findViewById(R.id.theearth);sea = (CheckBox)findViewById(R.id.sea);submit = (Button)findViewById(R.id.submit);//為單項選擇添加事件。rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {     if(checkedId == sun.getId()){                        text +=sun.getText().toString();     }else if(checkedId == moon.getId()){     text +=moon.getText().toString();     }else{     text +=stars.getText().toString();     }}});//多項選擇的事件唯寫一個。那兩個可以根據自己的需求來填寫。day.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(day.isChecked()){displayToast("你喜歡"+day.getText());}}});//添加按鈕點擊事件,用多士來顯示選中內容。submit.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String str="喜歡";if(day.isChecked()){str += day.getText()+"、";}if(theearth.isChecked()){str += theearth.getText()+"、";}if(sea.isChecked()){str += sea.getText()+"、";}displayToast(name.getText().toString()+str+text);}});}    //多士,資訊提示。public void displayToast(String text){Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();}}


運行圖片:

當選中天多項選擇時,則會觸發事件彈出多士資訊。


當輸入名字和單項選擇之後點擊提交按鈕,就會觸發事件,提示你選中的資訊。



二.單擊事件:

單擊事件有三種常用處理方式:

1).匿名內部類作為事件監聽:

實現:

按鈕:


代碼:

        Button button;button = (Button)findViewById(R.id.click);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {displayToast("點擊按鈕");}});

運行圖片:



2).內部類作為監聽器:

按鈕:


代碼:

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);    Button button;button = (Button)findViewById(R.id.click);button.setOnClickListener(new ButtonOnClick());}private final class ButtonOnClick implements OnClickListener{@Overridepublic void onClick(View v) {displayToast("點擊按鈕");}}public void displayToast(String text){Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

運行圖片:




3).在控制項中指定監聽方法:

android:onClick="指定方法名字"

按鈕:

 

代碼:

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);    Button button;button = (Button)findViewById(R.id.click);}//必須按這樣的格式,方法名和參數名可以修改其他不能改動。public void onClick(View v){displayToast("我已經點擊了按鈕");}public void displayToast(String text){Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
運行圖片



還有其他事件監聽方式,想瞭解的大家可以去查。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.