心情小品:
最近一直在趕自己的小作品 很久沒有更新了,
趕快把最近學到 比較特別、創意的功能 跟大家分享一下,
而弄到這個時候 我想 我真的是愛上APP了 XD。
Android 元件 Intent
1.內建語音應用:
使用介紹:如何開啟內建的語音功能
簡單敘述:開啟語音功能後,說的話設定到文字中
關鍵程式碼 :聲音就是我們自己的意圖
傳遞:我拿錢給老闆
Intent intent1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(intent1, RequestCode1);
|
回乎涵式: 老闆要把商品拿給我
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RequestCode1 && resultCode == RESULT_OK) {
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String message = text.get(0);
}
|
我們開始來實現這個功能吧
詳細位置:
Layout:(新增一個 EditText 、Button )
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.andy.blog.MainActivity">
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入文字" />
<Button
android:id="@+id/btn_語音功能"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="語音功能" />
</LinearLayout>
|
JAVA:
|
package com.example.andy.blog;
// Android 元件(Intent) 如何使用意圖、語音辨識功能、上Google查詢資料
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//意圖 RequestCode的代號
private final int RequestCode1 = 100;
//宣告
private EditText m_ed_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//當程式進入 onCreate 階段時,就執行這個方法
findID();
}
//尋找ID
private void findID() {
m_ed_text = (EditText) findViewById(R.id.et_text);
}
//按下按鈕
public void onclick(View view) {
//建立一個意圖,Intent(Andorid內建的方法)
//不用put參數,因為你的嘴巴就是傳遞過去的參數
Intent intent1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//開始執行 並得到結果,startActivityForResult(意圖,意圖的請求代碼)
startActivityForResult(intent1, RequestCode1);
}
//需要一個具有回應的意圖,建立一個回乎涵式
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//當我的 請求代碼 為 剛剛傳送意圖的代碼
//結果的代碼 為 RESULT_OK
if (requestCode == RequestCode1 && resultCode == RESULT_OK) {
//回傳是一個 ArrayList<String>的陣列,且資訊都放在第0項
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//得到第一項的值
String message = text.get(0);
//設定Edtext的文字
m_ed_text.setText(message);
}
}
}
|
我們來看看成果吧
2.Google搜尋應用:
使用介紹:開啟google內建搜尋功能
簡單敘述:輸入的文字,直接跳到google搜尋的頁面
關鍵程式碼 :
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY,"searchString") startActivity(intent); |
我們開始來實現這個功能吧
詳細位置:
Layout:再布局中 再新增一個按鈕,"Google搜尋功能"
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.andy.blog.MainActivity">
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入文字" />
<Button
android:id="@+id/btn_語音功能"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="語音功能" />
<Button
android:id="@+id/btn_Google搜尋功能"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="Google搜尋功能" />
</LinearLayout>
|
JAVA:
|
package com.example.andy.blog;
// Android 元件(Intent) 如何使用意圖、語音辨識功能、上Google查詢資料
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private final int RequestCode1 = 100;
private EditText m_ed_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findID();
}
private void findID() {
m_ed_text = (EditText) findViewById(R.id.et_text);
}
public void onclick(View view) {
switch (view.getId()) {
case R.id.btn_語音功能:
Intent intent1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(intent1, RequestCode1);
break;
case R.id.btn_Google搜尋功能:
//新增一個意圖
Intent intent2 = new Intent();
// 隱性方法的調用, setAction(Andorid內建的方法內建的方法);
// 例如 上篇講到的 setAction("help");
// (Intent.ACTION_WEB_SEARCH) 我們ctrl點進去,發現其實是一個字串
// 代表說,一定有個類別可以接收這個字串的過濾器
intent2.setAction(Intent.ACTION_WEB_SEARCH);
//意圖 放入資料
// SearchManager.QUERY =資料的識別碼
// "" =可以輸入要收尋的字串
intent2.putExtra(SearchManager.QUERY, "我喜歡android");
//開始
startActivity(intent2);
break;
}
}
//需要一個具有回應的意圖,建立一個回乎涵式
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//當我的 請求代碼 為 剛剛傳送意圖的代碼
//結果的代碼 為 RESULT_OK
if (requestCode == RequestCode1 && resultCode == RESULT_OK) {
//回傳是一個 ArrayList<String>的陣列,且資訊都放在第0項
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//得到第一項的值
String message = text.get(0);
//設定Edtext的文字
m_ed_text.setText(message);
}
}
}
|
我們來看看成果吧
3.語音功能 和 Google搜尋 的相互應用:
使用介紹:語音功能 和 搜尋功能 相互使用
簡單敘述:我說話 ,然後馬上啟動 搜尋的功能
我們開始來實現這個功能吧
詳細位置:
JAVA:
|
package com.example.andy.blog;
// Android 元件(Intent) 如何使用意圖、語音辨識功能、上Google查詢資料
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private final int RequestCode1 = 100;
private EditText m_ed_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findID();
}
private void findID() {
m_ed_text = (EditText) findViewById(R.id.et_text);
}
public void onclick(View view) {
switch (view.getId()) {
case R.id.btn_語音功能:
Intent intent1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(intent1, RequestCode1);
break;
case R.id.btn_Google搜尋功能:
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_WEB_SEARCH);
intent2.putExtra(SearchManager.QUERY, "我喜歡android");
startActivity(intent2);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RequestCode1 && resultCode == RESULT_OK) {
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//取得語音功能回傳的字串
String message = text.get(0);
//馬上再新增一個意圖
Intent intent3 = new Intent();
//調用隱性的方法
intent3.setAction(Intent.ACTION_WEB_SEARCH);
//放入 (資源的識別碼,資源為message)
intent3.putExtra(SearchManager.QUERY, message);
//開始
startActivity(intent3);
//Edtext設定文字,內容為message
m_ed_text.setText(message);
}
}
}
|
我們來看看成果吧
沒有留言:
張貼留言