安卓是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的操作系统。主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。Android操作系统最初由Andy Rubin开发,主要支持手机。2005年8月由Google收购注资。2007年11月,Google与84家硬件制造商、软件开发商及电信营运商组建开放手机联盟共同研发改良Android系统。随后Google以Apache开源许可证的授权方式,发布了Android的源代码。第一部Android智能手机发布于2008年10月。
指定应用的包名
package="com.itheima.helloworld" data/data/com.itheima.helloworld(上面代码指定的包名)应用生成的文件都会存放在此路径下Android的四大组件在使用前全部需要在清单文件中配置
的配置对整个应用生效
的配置对该activity生效
功能:用户输入一个号码,点击拨打按钮,启动系统打电话的应用把号码拨打出去
组件必须设置宽高,否则不能通过编译
android:layout_width="wrap_content" android:layout_height="wrap_content"如果要在java代码中操作某个组件,则组件需要设置id,这样才能在代码中通过id拿到这个组件
android:id="@+id/et_phone"给按钮设置侦听
//通过id拿到按钮对象 Button bt_call = (Button) findViewById(R.id.bt_call); //给按钮设置点击 bt_call.setOnClickListener(new MyListener());Android系统中基于动作机制,来调用系统的应用,你告诉系统你想做什么动作,系统就会把能做这个动作的应用给你,如果没有这个应用,会抛异常
设置动作,通过意图告知系统
//把号码打出去 //先创建一个意图对象 Intent intent = new Intent(); //设置动作,打电话 intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + phone)); //把意图告诉系统 startActivity(intent);添加权限
<uses-permission android:name="android.permission.CALL_PHONE"/>@源码 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.myapplication"> <uses-permission android:name="android.permission.CALL_PHONE"/> <application android:allowBackup="true" android:icon="@mipmap/chrome" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入电话号码"/> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="132313123135" /> <Button android:id="@+id/bt_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拨打"/> </LinearLayout>MainActivity
package com.example.administrator.myapplication; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //给按钮设置监听 //得到按钮对象 Button button = findViewById(R.id.bt_call); //设置监听 button.setOnClickListener(new MyListener()); } class MyListener implements View.OnClickListener{ @Override public void onClick(View v) { System.out.println("按钮被点击了"); EditText editText = findViewById(R.id.et_phone); String phone = editText.getText().toString(); //创建意图对象 Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+phone)); //把动作告诉系统 startActivity(intent); } } }@效果演示
定义一个MyListener实现onClickListener接口
Button bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(new MyListener());定义一个匿名内部类实现onClickListener接口
Button bt2 = (Button) findViewById(R.id.bt2); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("第二种"); } });让当前activity实现onClickListener接口
Button bt3 = (Button) findViewById(R.id.bt3); bt3.setOnClickListener(this);给Button节点设置onClick属性,
android:onClick="click"然后在activity中定义跟该属性值同名的方法
public void click(View v){ System.out.println("第四种"); }功能:用户输入号码和短信内容,点击发送按钮,调用短信api把短信发送给指定号码
输入框的提示
android:hint="请输入号码"添加权限
<uses-permission android:name="android.permission.SEND_SMS"/>如果短信过长,需要拆分
List<String> smss = sm.divideMessage(content);