這是我的代碼片段沒有錯誤,當它不工作! 每個片段具有語音識別按鈕,inputText的其中接收文本,並給定文本進行比較與inputText的,並強調它紅色如果不匹配..,片段活動不起作用
public class LayoutOne extends Fragment {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView inputText;
private TextView givenText;
public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();
return f;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.layout_one);
inputText = (TextView)getView().findViewById(R.id.inputText);
givenText = (TextView)getView().findViewById(R.id.givenText);
btnSpeak = (ImageButton)getView().findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try{
startActivityForResult(intent, RESULT_SPEECH);
inputText.setText("");
}catch(ActivityNotFoundException a) {
}
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup v = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == -1 && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
inputText.setText(text.get(0));
if(givenText.getText().toString().equalsIgnoreCase(inputText.getText().toString())){
inputText.setBackgroundColor(Color.parseColor("BLUE"));
}
else{
{inputText.setBackgroundColor(Color.parseColor("RED"));}
}
}
break;
}
}
}
}
什麼想法?
你有一個logcat錯誤stacktrace?嘗試在你的代碼周圍放入一些Log.v(「Test」,「test」),看看你能從中得到什麼信息。 – Tukajo
另外,發佈您的片段充滿的活動。這僅僅是一個片段,而不是一個片段活動。你不會在代碼中的任何地方膨脹這個片段,我可以看到 – Tukajo
的可讀性:'resultCode == -1'應該最好是'resultCode == RESULT_OK' – donfuxx