我的xml布局中有一个电话号码,我希望它可以点击,以便它会自动输入短信服务,我可以发送短信到该号码。
Alinas.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:background="#ffffff">
<ImageView
android:layout_width="160dp"
android:layout_height="140dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/alina_sbakerycafe"
android:id="@+id/imageView" />
<TextView
android:layout_width="2500dp"
android:layout_height="50dp"
android:text="Alina's Bakery Cafe"
android:textStyle="bold"
android:textSize="25dp"
android:gravity="end"
android:textAlignment="textEnd"
android:layout_alignTop="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
<TextView
android:layout_width="300dp"
android:layout_height="50dp"
android:text="Address: New Baneswor, Kathmandu Phone No.:\t 9841123456"
android:textAlignment="center"
android:textSize="15dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="@android:dimen/thumbnail_width"
android:layout_height="@android:dimen/app_icon_size"
android:onClick="moveToActivityViewMenu"
android:background="@drawable/button"
android:text="View Menu"
android:textColor="#ffffff"
android:layout_marginBottom="83dp"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
点击电话号码后,它会自动输入到此处:
SendSMSActivity.java
package com.golo.acer.mrestro4;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SendSmsActivity extends Activity {
Button sendSmsBtn;
EditText toPhoneNumber;
EditText smsMessageET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_sms);
sendSmsBtn = (Button) findViewById(R.id.btnSendSMS);
toPhoneNumber = (EditText) findViewById(R.id.editTextPhoneNo);
smsMessageET = (EditText) findViewById(R.id.editTextSMS);
sendSmsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSms();
}
});
}
private void sendSms() {
String toPhone = toPhoneNumber.getText().toString();
String smsMessage = smsMessageET.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(toPhone, null, smsMessage, null, null);
Toast.makeText(this, "SMS sent", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void goToInbox(View v) {
Intent intent = new Intent(SendSmsActivity.this, ReceiveSMSActivity.class);
startActivity(intent);
}
public void moveToSmsActivity(View view) {
Intent intent = new Intent(this, SmsActivity.class);
startActivity(intent);
}
}
答案 0 :(得分:1)
你可以用这个
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
for (Object aPdusObj : pdusObj) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
String senderAddress = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
Log.e(TAG, "Received SMS: " + message + ", Sender: " + senderAddress);
String verificationCode = getVerificationCode(message);
Log.e(TAG, "OTP received: " + verificationCode);
setdata(verificationCode);
Intent hhtpIntent = new Intent(context, OTPVerificationActivity.class);
hhtpIntent.putExtra("otp", verificationCode);
context.startService(hhtpIntent);
}
}
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
/**
* Getting the OTP from sms message body
* ':' is the separator of OTP from the message
*
* @param message
* @return
*/
private String getVerificationCode(String message) {
String code = null;
code = message.substring(0, 6);
return code;
}}
答案 1 :(得分:1)
首先为该电话号码textview添加一个属性android:clickable = true
TextView textView = (TextView) findViewById(R.id.phoneNumber);
然后添加
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(this,SendSmsActivity.class);
// send phone number to sms activity
intent.putExtra("phonenumber",textView.getText().toString())
startActivity(intent);
}
});
在短信活动中
Bundle bundle=getIntent().getExtras();
editText=(EditText)findViewById(R.id.phoneNumber);
editText.setText(bundle.getString("phonenumber"));