android應用開發詳解(六)--------------事件處理機制(續)

來源:互聯網
上載者:User

標籤:android應用

1、工程目錄



2、TestEventActivity.java


package com.example.test_event;import android.os.Bundle;import android.app.Activity;import android.view.KeyEvent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnFocusChangeListener;import android.view.View.OnKeyListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;//import android.widget.TextView;import android.widget.Toast;public class TestEventActivity extends Activity {// private TextView myTextview1, myTextview2, myTextView3;private Button myButton1, myButton2;private EditText myEditText1, myEditText2;private CheckBox myCheckbox1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test_event);// myTextview1 = (TextView) findViewById(R.id.textviewLayout01);// myTextview2 = (TextView) findViewById(R.id.textviewLayout02);// myTextView3 = (TextView) findViewById(R.id.textviewLayout03);myButton1 = (Button) findViewById(R.id.buttonLayout01);myButton2 = (Button) findViewById(R.id.buttonLayout02);myEditText1 = (EditText) findViewById(R.id.edittextLayout01);myEditText2 = (EditText) findViewById(R.id.editviewLayout02);myCheckbox1 = (CheckBox) findViewById(R.id.checkboxLayout01);// 編輯文字框的按鍵事件myEditText1.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View arg0, int arg1, KeyEvent arg2) {// TODO Auto-generated method stubmyEditText1.setText("");return false;}});myEditText2.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View arg0, int arg1, KeyEvent arg2) {// TODO Auto-generated method stubmyEditText2.setText("");return false;}});// 編輯文字框的焦時間點事件myEditText1.setOnFocusChangeListener(new OnFocusChangeListener() {@Overridepublic void onFocusChange(View arg0, boolean arg1) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), myEditText1.getText(),Toast.LENGTH_LONG).show();}});myEditText2.setOnFocusChangeListener(new OnFocusChangeListener() {@Overridepublic void onFocusChange(View arg0, boolean arg1) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), myEditText2.getText(),Toast.LENGTH_LONG).show();}});// 多選框的選擇事件myCheckbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(),myCheckbox1.isChecked() + "", Toast.LENGTH_LONG).show();}});// 按鈕的選擇事件myButton1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), myButton1.getText(),Toast.LENGTH_LONG).show();}});myButton2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), myButton2.getText(),Toast.LENGTH_LONG).show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.test_event, menu);return true;}}

3、布局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TableLayout        android:id="@+id/myTableLayout"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <TableRow>            <TextView                android:id="@+id/textviewLayout01"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="使用者名稱稱" />            <EditText                android:id="@+id/edittextLayout01"                android:layout_width="fill_parent"                android:scrollHorizontally="true"                android:text="admin" />        </TableRow>        <TableRow>            <TextView                android:id="@+id/textviewLayout02"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="使用者密碼" />            <EditText                android:id="@+id/editviewLayout02"                android:layout_width="fill_parent"                android:password="true"                android:scrollHorizontally="true"                android:text="123" />        </TableRow>        <TableRow>            <TextView                android:id="@+id/textviewLayout03"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="自動登入" />            <CheckBox                android:id="@+id/checkboxLayout01"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />        </TableRow>        <TableRow>            <Button                android:id="@+id/buttonLayout01"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="登入" />            <Button                android:id="@+id/buttonLayout02"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="取消" />        </TableRow>    </TableLayout></LinearLayout>

【知識】

android中常見的事件監聽器:

(1)點擊事件View.onClickListener:當使用者碰觸到某個組件或者方向鍵被按下時產生該事件,處理方法是onClick()

(2)焦時間點事件View.OnFocusChangeListener:組件得到或者失去焦點時產生該事件,處理方法是onFocusChange()

(3)按鍵事件View.OnKeyListener:使用者按下或者釋放裝置上的某個按鍵時產生,處理方法onKey()

(4)碰觸事件View.OnTouchListener:裝置具有碰觸功能時,碰觸螢幕產生該事件,處理方法onTouch()

(5)建立操作功能表事件View.OnCreateContextMenuListener:建立操作功能表時產生該事件,處理方法是onCreateContextMenu()

事件處理步驟:

(1)建立事件監聽器

(2)給要響應事件的組件註冊事件監聽器

(3)在事件處理方法中編寫實現代碼

android應用開發詳解(六)--------------事件處理機制(續)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.