點擊事件的必備條件:實現 介面,重寫 方法 以撥號簡單案例為例,如下圖效果: 邏輯流程: 1. 獲取點擊對象,獲取數據 2. 給對象設置監聽類 3. 實現 介面,重寫 方法 邏輯: 1. 獲取輸入內容:使用 對象的 方法,該方法返回值為 ,需要用 轉換成 類型 2. 判斷輸入是否為空, 方法判斷字元 ...
點擊事件的必備條件:實現OnClickListener
介面,重寫onclick(View v)
方法
以撥號簡單案例為例,如下圖效果:
邏輯流程:
- 獲取點擊對象,獲取數據
- 給對象設置監聽類
- 實現
OnClickListener
介面,重寫onClick(View v)
方法
onClick(View v)
邏輯:
獲取輸入內容:使用
EditText
對象的getText()
方法,該方法返回值為Editable
,需要用toString()
轉換成String
類型判斷輸入是否為空,
TextUtils.isEmpty(str)
方法判斷字元串str
是否為空為空則提示
號碼不能為空
,使用Toast.makeText(MainActivity.this, "號碼不能為空", Toast.LENGTH_SHORT).show();
,
Toast makeText (Context context, CharSequence text, int duration)
返回值為Toast,參數列表(Context對象,提示內容,顯示時長)
- 如果不為空,
new
一個Intent
對象,使用Intent
對象的setAction(String action)
方法設置動作,使用Intent
對象的setData(Uri uriData)
方法設置數據。startActivity(Intent intent)
方法啟動Activity。
註:應該在程式清單文件AndroidManifest.xml中添加<uses-permission android:name="android.permission.CALL_PHONE"/>
,撥打電話時,美國緊急電話不會直接撥打
三種方法:第四種不推薦,前三種酌情使用
1. 內部實現類
佈局文件:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="在此輸入電話號碼">
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="14dp"
android:text="撥打此號碼" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:text="Button3" />
</RelativeLayout>
MainActivity.java
package com.example.onclick1;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.storage.OnObbStateChangeListener;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
//設置監聽類
button1.setOnClickListener(new MyOnClickListener());
button2.setOnClickListener(new MyOnClickListener());
button3.setOnClickListener(new MyOnClickListener());
}
private class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
int i = v.getId();
switch (i) {
case R.id.button1:
String number = editText.getText().toString();
if (TextUtils.isEmpty(number)) {
Toast.makeText(MainActivity.this, "號碼不能為空", Toast.LENGTH_SHORT).show();
}else {
Intent intent = new Intent();
intent.setAction(intent.ACTION_CALL);
Uri data = Uri.parse("tel:"+number);
intent.setData(data);
startActivity(intent);
}
break;
case R.id.button2:
Toast.makeText(MainActivity.this, "你點擊了Button2", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(MainActivity.this, "你點擊了Button3", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
2. 內部匿名類
MainActivity.java
package com.example.onclick1;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.storage.OnObbStateChangeListener;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
//設置監聽類
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String number = editText.getText().toString();
if(TextUtils.isEmpty(number)){
Toast.makeText(MainActivity.this, "[內部匿名類]號碼不能為空", Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent();
intent.setAction(intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "[內部匿名類]你點擊了Button2", Toast.LENGTH_SHORT).show();
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "[內部匿名類]你又點擊了button3", Toast.LENGTH_SHORT).show();
}
});
}
}
3. Activity實現OnClickLitener
MainActivity.java
package com.example.onclick1;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.storage.OnObbStateChangeListener;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
//設置監聽類
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.button1:
String number = editText.getText().toString();
if (TextUtils.isEmpty(number)) {
Toast.makeText(this, "[Activity實現類]號碼不能為空", Toast.LENGTH_SHORT).show();
}else {
Intent intent = new Intent();
intent.setAction(intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
break;
case R.id.button2:
Toast.makeText(this, "[Activity實現類]你點擊了Button2", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(this, "[Activity實現類]你點擊了Button3", Toast.LENGTH_SHORT).show();
break;
}
}
}
4. 結合layout文件聲明方法
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="在此輸入電話號碼">
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="14dp"
android:onClick="button"
android:text="撥打此號碼" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:onClick="button"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:onClick="button"
android:text="Button3" />
</RelativeLayout>
MainActivity.java
package com.example.onclick1;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.storage.OnObbStateChangeListener;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button(View v) {
EditText editText = (EditText) findViewById(R.id.editText1);
switch (v.getId()) {
case R.id.button1:
String number = editText.getText().toString();
if (TextUtils.isEmpty(number)) {
Toast.makeText(this, "[佈局文件中Button的android:onClick屬性]號碼不能為空", Toast.LENGTH_SHORT).show();
}else {
Intent intent = new Intent();
intent.setAction(intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
break;
case R.id.button2:
Toast.makeText(this, "[佈局文件中Button的android:onClick屬性]你點擊了Button2", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(this, "[佈局文件中Button的android:onClick屬性]你點擊了Button3", Toast.LENGTH_SHORT).show();
break;
}
}
}