मेरे पास एक व्यूपर है और मुझे वहां एक बटन का उपयोग करने की आवश्यकता है। बटन को आवाज को पहचानना है, और इसे टेक्स्ट व्यू में दिखाना है। मैं VoiceRecognition और ViewPager के लिए एक ही लेआउट लागू करता हूं।ViewPager में बटन को कैसे कार्यान्वित करें?
सवाल यह है कि क्लिक के बाद वॉयस रिकॉग्निशन चलाने के लिए बटन को सक्रिय करने के लिए कैसे करें। मैं बटन और टेक्स्ट व्यू पर एक टैग सेट करने का प्रयास करता हूं, लेकिन मैं यह गलत करता हूं, यह काम नहीं करता है।
ViewPager
:
public class SwipeAdapter extends PagerAdapter{
private int[] car = {R.string.car1, R.string.car2,
R.string.car3, R.string.car4, R.string.car5};
private Context context;
private LayoutInflater layoutInflater;
public SwipeAdapter(Context context){
this.context = context;
}
@Override
public int getCount() {
return car.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(RelativeLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = layoutInflater.inflate(R.layout.carSwipe, container, false);
//Implement the Button
Button carBut = (Button)itemView.findViewById(R.id.buttonCar);
carButton.setTag("car");
TextView textView = (TextView) itemView.findViewById(R.id.interTextView);
textView.setTag("text");
textView.setText(car[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
}
ध्वनि पहचान
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carSwipe);
Button recognizeButton = (Button)findViewById(R.id.button1);
recognizeButton.setOnClickListener(this);
}
@Override
public void setContentView(View context) {
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
final Configuration conf = context.getResources().getConfiguration();
conf.locale = new Locale("en");
context.getResources().updateConfiguration(conf, dm);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
ArrayList<String> results;
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//Set a tag here
TextView speechText = (TextView) mPager.findViewWithTag("text" + results);
String str="";
for(int i=0;i<results.size();i++){
str+= results.get(i);
}
speechText.setText(str);
}
}
}
एक्सएमएल
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center|center_horizontal"
android:id="@+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/interTextView" />
</LinearLayout>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="recognition"
android:id="@+id/buttonRec"
android:layout_above="@+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
क्या भाषण मान्यता दर्शक के बिना स्वयं ही काम करती है? जैसे आप इसे पहचानने वाले शब्दों को लॉग कर सकते हैं? –
क्या यह टुकड़ों का उपयोग करने के लिए स्विच करने का विकल्प है? फिर आप FragmentStatePagerAdapter से विस्तार कर सकते हैं और "getItem (int position)" में सही टुकड़ा वापस कर सकते हैं। जब बटन दबाया जाता है और परिणाम का जवाब दिया जाता है तो वह टुकड़ा आसानी से सही गतिविधि शुरू कर सकता है। –
इस ट्यूटोरियल पर एक नज़र डालें: https://developer.android.com/training/animation/screen-slide.html (SubClassing PagerAdapter केवल FragmentStatePagerAdapter या FragmentPagerAdapter का उपयोग करने से कहीं अधिक कठिन है और इसलिए एक अनुशंसित दृष्टिकोण नहीं है) –