问题描述:

this is my code for the fragment there is no error put when it doesn't work !

each fragment have voice recognition button ,inputText which receive text ,and given text to make comparison with the inputText and highlight it Red if it is not match ..,

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;

}

}

}

}

any Idea ?

网友答案:

First you need to create your fragment class differently as you do not have an inflater, a fragment doesn't use the generic onCreate as other android classes...

Public class myFragment extends Fragment{

TextView testTextView;
View myFragmentView
    public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
            Bundle SavedInstantState) {

        myFragmentView = viewInflation.inflate(
                R.layout.myfragment_layout, container, false);
            testTextView = myFragmentView.findViewById(R.id.testTextView);
   }
}

That is a basic example of how your fragment should be set up.... then you can inflate it in a FragmentActivity using a transaction manager and creating a field of your Fragment's class.

[update]

Also, if you use eclipse's visual editor you can just drag and drop a fragment from the visual menu and it should just ask you what fragment class you want to attach to it.

相关阅读:
Top