作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在谷歌云语音API中使用recycleview。我想将回收 View 中的字符串放入单个编辑文本中。如何将字符串从回收 View 移动到单个文本中?
enter code here
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.android.speech;
import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.icu.util.ValueIterator;
import android.os.Bundle;
import android.os.IBinder;
import android.sax.Element;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
MessageDialogFragment.Listener {
static EditText edit1;
static EditText edit2;
static EditText edit3;
static String a1,a2;
static String[] b1,b2;
static int count=0;
Button btn;
private static final String FRAGMENT_MESSAGE_DIALOG = "message_dialog";
private static final String STATE_RESULTS = "results";
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 1;
private SpeechService mSpeechService;
private VoiceRecorder mVoiceRecorder;
private final VoiceRecorder.Callback mVoiceCallback = new
VoiceRecorder.Callback() {
@Override
public void onVoiceStart() {
showStatus(true);
if (mSpeechService != null) {
mSpeechService.startRecognizing(mVoiceRecorder.getSampleRate());
}
}
@Override
public void onVoice(byte[] data, int size) {
if (mSpeechService != null) {
mSpeechService.recognize(data, size);
}
}
@Override
public void onVoiceEnd() {
showStatus(false);
if (mSpeechService != null) {
mSpeechService.finishRecognizing();
}
}
};
// Resource caches
private int mColorHearing;
private int mColorNotHearing;
// View references
private TextView mStatus;
private TextView mText;
private ResultAdapter mAdapter;
private RecyclerView mRecyclerView;
private final ServiceConnection mServiceConnection = new
ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName componentName, IBinder
binder) {
mSpeechService = SpeechService.from(binder);
mSpeechService.addListener(mSpeechServiceListener);
mStatus.setVisibility(View.VISIBLE);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mSpeechService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit1=(EditText) findViewById(R.id.edit1);
edit2=(EditText) findViewById(R.id.edit2);
edit3=(EditText) findViewById(R.id.edit3);
btn=(Button)findViewById(R.id.button);
final Resources resources = getResources();
final Resources.Theme theme = getTheme();
mColorHearing = ResourcesCompat.getColor(resources,
R.color.status_hearing, theme);
mColorNotHearing = ResourcesCompat.getColor(resources,
R.color.status_not_hearing, theme);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
mStatus = (TextView) findViewById(R.id.status);
mText = (TextView) findViewById(R.id.text);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
final ArrayList<String> results = savedInstanceState == null ? null :
savedInstanceState.getStringArrayList(STATE_RESULTS);
mAdapter = new ResultAdapter(results);
mRecyclerView.setAdapter(mAdapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
protected void onStart() {
super.onStart();
// Prepare Cloud Speech API
bindService(new Intent(this, SpeechService.class), mServiceConnection,
BIND_AUTO_CREATE);
// Start listening to voices
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED) {
startVoiceRecorder();
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECORD_AUDIO)) {
showPermissionMessageDialog();
} else {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.RECORD_AUDIO},
REQUEST_RECORD_AUDIO_PERMISSION);
}
}
@Override
protected void onStop() {
// Stop listening to voice
stopVoiceRecorder();
// Stop Cloud Speech API
mSpeechService.removeListener(mSpeechServiceListener);
unbindService(mServiceConnection);
mSpeechService = null;
super.onStop();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mAdapter != null) {
outState.putStringArrayList(STATE_RESULTS, mAdapter.getResults());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) {
if (permissions.length == 1 && grantResults.length == 1
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startVoiceRecorder();
} else {
showPermissionMessageDialog();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_file:
mSpeechService.recognizeInputStream
(getResources().openRawResource(R.raw.audio));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void startVoiceRecorder() {
if (mVoiceRecorder != null) {
mVoiceRecorder.stop();
}
mVoiceRecorder = new VoiceRecorder(mVoiceCallback);
mVoiceRecorder.start();
}
private void stopVoiceRecorder() {
if (mVoiceRecorder != null) {
mVoiceRecorder.stop();
mVoiceRecorder = null;
}
}
private void showPermissionMessageDialog() {
MessageDialogFragment
.newInstance(getString(R.string.permission_message))
.show(getSupportFragmentManager(), FRAGMENT_MESSAGE_DIALOG);
}
private void showStatus(final boolean hearingVoice) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mStatus.setTextColor(hearingVoice ? mColorHearing :
mColorNotHearing);
}
});
}
@Override
public void onMessageDialogDismissed() {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.RECORD_AUDIO},
REQUEST_RECORD_AUDIO_PERMISSION);
}
private final SpeechService.Listener mSpeechServiceListener =
new SpeechService.Listener() {
@Override
public void onSpeechRecognized(final String text, final boolean
isFinal) {
if (isFinal) {
mVoiceRecorder.dismiss();
}
if (mText != null && !TextUtils.isEmpty(text)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (isFinal) {
mText.setText(null);
mAdapter.addResult(text);
mRecyclerView.smoothScrollToPosition(0);
} else {
mText.setText(text);
}
}
});
}
}
};
private static class ViewHolder extends RecyclerView.ViewHolder {
TextView text;
ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.item_result, parent, false));
text = (TextView) itemView.findViewById(R.id.text);
}
}
private static class ResultAdapter extends
RecyclerView.Adapter<ViewHolder> {
private final ArrayList<String> mResults = new ArrayList<>();
ResultAdapter(ArrayList<String> results) {
if (results != null) {
mResults.addAll(results);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()),
parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.text.setText(mResults.get(position));
}
@Override
public int getItemCount() {
return mResults.size();
}
void addResult(String result) {
mResults.add(0, result);
notifyItemInserted(0);
}
public ArrayList<String> getResults() {
return mResults;
}
}
}
这是java代码。如果你知道答案,请告诉我..我尝试过,但没有成功。 editview 不显示 recycleview 中的所有文本...
最佳答案
在该回收 View 之外创建一个 TextView 元素。然后在onCreate()方法中对其进行初始化。
您需要从该回收 View 中提取文本。我不知道你持有数据。不管怎样,从中得到一条短信。然后当 recycleText 更新时使用 textView.setText(...) updateText
在mainActivity中这样定义TextView并在onCreate中初始化它
static TextView recyText;
在
oncreate(){
....
recyText=(TextView) findViewById(R.id.recyText);
}
在下面的代码段中更新它
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.text.setText(mResults.get(position));
recyText.setText(recyText.getText()+ " " +mResults.get(position))
}
关于java - RecyclerView 到 EditTtext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53028776/
我在谷歌云语音API中使用recycleview。我想将回收 View 中的字符串放入单个编辑文本中。如何将字符串从回收 View 移动到单个文本中? enter code here /* * Cop
我是一名优秀的程序员,十分优秀!