activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="Phone number:"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
/>
<EditText
android:id="@+id/edt_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:gravity="center"
android:textColor="#ff0000"
android:layout_weight="1"
android:textSize="20sp"
/>
</LinearLayout>
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Call" />
</FrameLayout>
MainActivity.java
Java Android 2017
package com.example.lonely.demophonecall;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText edtNum;
private Button btnCall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtNum = (EditText)findViewById(R.id.edt_num);
btnCall = (Button)findViewById(R.id.btn_call);
btnCall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_call:
//TO DO
Intent intent = new Intent(android.content.Intent.ACTION_DIAL, Uri.parse("tel:"+edtNum.getEditableText().toString()));
startActivity(intent);
break;
default:
break;
}
}
}
AndroidManifest.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lonely.demophonecall">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<uses-permission android:name="android.permission.CALL_PHONE" />
</manifest>
0 nhận xét:
Post a Comment