Android----View對象的點擊事件處理方法

來源:互聯網
上載者:User

package com.example.test;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{Button bt_call;private EditText tv_phonenumber;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//載入布局檔案setContentView(R.layout.activity_main);//尋找按鈕    bt_call = (Button) findViewById(R.id.call);    //尋找文字框    tv_phonenumber = (EditText) MainActivity.this.findViewById(R.id.et_phonenumber);        //給按鈕設定點擊事件  1.建立一個內部類定義點擊事件    //bt_call.setOnClickListener(new mylisnter());       /* bt_call.setOnClickListener(new OnClickListener() {//2.採用匿名內部類建立點擊事件@Overridepublic void onClick(View v) {callphone();}});*/        //3.讓activity實現點擊事件的介面    //bt_call.setOnClickListener(this);}//4.在布局檔案裡綁定一個點擊的方法public void callButtonClicked(View view){callphone();}private class mylisnter implements OnClickListener{/* * 當按鈕被點擊的時候,調用的方法 *  */@Overridepublic void onClick(View v) {callphone();}}private void callphone() {String number = tv_phonenumber.getText().toString().trim();if(TextUtils.isEmpty(number)){Toast.makeText(MainActivity.this, "請輸入電話號碼", Toast.LENGTH_SHORT).show();return;}//意圖,想幹一件什麼事情Intent intent = new Intent();intent.setAction(intent.ACTION_CALL);intent.setData(Uri.parse("tel:"+number));startActivity(intent);}//介面裡面未實現的方法@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.call:callphone();break;}}}

layout

                android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@id/et_phonenumber"        android:layout_marginTop="16dp"        android:text="@string/bt_call"/>


聯繫我們

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