activity_main.xml样式界面(样式代码中有两个小图标,可自定义,底部有上传该小图标)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingRight="10dp" android:paddingLeft="10dp" android:paddingTop="10dp" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="40dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="张三"/> <TextView android:id="@+id/mainActivity_text_phone" android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="13800138000"/> <ImageButton android:id="@+id/mainActivity_imgBtn_phone" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/phone" android:scaleType="fitXY" android:background="#0000"/> <ImageButton android:id="@+id/mainActivity_imgBtn_send" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/senc" android:scaleType="fitXY" android:background="#0000"/> </LinearLayout> </LinearLayout>
package com.example.phoneorsend; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private TextView phone; private ImageButton btnPhone,btnSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ phone = (TextView)findViewById(R.id.mainActivity_text_phone); btnSend = (ImageButton)findViewById(R.id.mainActivity_imgBtn_send); btnPhone = (ImageButton)findViewById(R.id.mainActivity_imgBtn_phone); btnSend.setOnClickListener(btn); btnPhone.setOnClickListener(btn); } View.OnClickListener btn = new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); ImageButton imageButton = (ImageButton)view; switch (imageButton.getId()){ //拨打电话 case R.id.mainActivity_imgBtn_phone: intent.setAction(intent.ACTION_DIAL);//跳转到拨号页面 intent.setData(Uri.parse("tel:"+phone.getText()));//拨打对象 startActivity(intent); break; //发送信息 case R.id.mainActivity_imgBtn_send: intent.setAction(intent.ACTION_SENDTO);//跳转带发送信息页面 intent.setData(Uri.parse("smsto:"+phone.getText())); //发送对象 intent.putExtra("sms_body","今天"+ getCycle()); //发送信息内容 startActivity(intent); break; } } }; //获取当前周期 private static String getCycle(){ Calendar cal=Calendar.getInstance(); String date = ""; switch ( cal.get(Calendar.DAY_OF_WEEK)){ case 1: date = "星期日"; break; case 2: date = "星期一"; break; case 3: date = "星期二"; break; case 4: date = "星期三"; break; case 5: date = "星期四"; break; case 6: date = "星期五"; break; case 7: date = "星期六"; break; } return date; } }