編輯:關於Android編程
在Android中,使用內部類可以在當前類裡面發用改監聽器類,因為監聽器類是外部類的內部類,所以可以自由訪問外部類的所有界面組件。以下是一個調用系統內部類實現短信發送的一個例子:
SMS類:
package com.example.msmlistener;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class SMS implements OnLongClickListener{
private Activity act;
private EditText add;
private EditText content;
public SMS(Activity act, EditText addr, EditText content){
this.act = act;
this.add = addr;
this.content = content;
}
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
String addressStr = add.getText().toString();
String contentStr = content.getText().toString();
SmsManager sm = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getBroadcast(act, 0, new Intent(), 0);
sm.sendTextMessage(addressStr, null, contentStr, sentIntent, null);
Toast.makeText(act, "短信發送完成!\n", Toast.LENGTH_LONG).show();
return false;
}
}
Activity XML:package com.example.msmlistener;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class SMS implements OnLongClickListener{
private Activity act;
private EditText add;
private EditText content;
public SMS(Activity act, EditText addr, EditText content){
this.act = act;
this.add = addr;
this.content = content;
}
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
String addressStr = add.getText().toString();
String contentStr = content.getText().toString();
SmsManager sm = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getBroadcast(act, 0, new Intent(), 0);
sm.sendTextMessage(addressStr, null, contentStr, sentIntent, null);
Toast.makeText(act, "短信發送完成!\n", Toast.LENGTH_LONG).show();
return false;
}
}
Activity JAVA:package com.example.msmlistener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText address;
EditText content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取頁面信息
address = (EditText)findViewById(R.id.address);
content = (EditText)findViewById(R.id.content);
Button btnsend = (Button)findViewById(R.id.send);
btnsend.setOnLongClickListener(new SMS(this, address, content));
}
}
注意需要添加發送短信的權限到Manfest中:
如果確實有多個實現監聽器需要實現相同的業務邏輯功能,可以考慮使用業務邏輯組件來定義業務邏輯功能,再讓事件監聽器來調用業務邏輯組件的業務邏輯方法。
現在app中,圖片預覽功能肯定是少不了的,用戶基本已經形成條件反射,看到小圖,點擊看大圖,看到大圖兩個手指開始進行放大,放大後,開始移動到指定部位。一、概述想要做到圖片支
RecyclerView已經出來很久了,許許多多的項目都開始從ListView轉戰RecyclerView,那麼,上拉加載和下拉刷新是一件很有必要的事情。在ListVie
如何添加覆蓋物,實現周邊搜索,以及對覆蓋物的點擊出現介紹等效果。效果圖:我們的需求是,當用戶點擊衣食住行,或者對對附近搜索是,從服務器返回數據(經緯度,商家信息,介紹等)
Android自帶的Email從6.3開始不支持exchange了,用了那麼久,突然不支持了還真是不習慣。市場上也沒有比較好的替代軟件,心想從網上搜一下能不