List of usage examples for android.view View requestFocus
public final boolean requestFocus()
From source file:android.support.v17.leanback.widget.GridWidgetTest.java
public void test27766012() throws Throwable { mInstrumentation = getInstrumentation(); Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class); intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.vertical_linear_with_button_onleft); intent.putExtra(GridActivity.EXTRA_CHILD_LAYOUT_ID, R.layout.horizontal_item); intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 2); intent.putExtra(GridActivity.EXTRA_STAGGERED, false); intent.putExtra(GridActivity.EXTRA_UPDATE_SIZE, false); initActivity(intent);/* w w w . ja v a 2 s. c o m*/ mOrientation = BaseGridView.VERTICAL; mNumRows = 1; // set remove animator two seconds mGridView.getItemAnimator().setRemoveDuration(2000); final View view = mGridView.getChildAt(1); runTestOnUiThread(new Runnable() { @Override public void run() { view.requestFocus(); } }); assertTrue(view.hasFocus()); runTestOnUiThread(new Runnable() { @Override public void run() { mActivity.removeItems(0, 2); } }); // wait one second, removing second view is still attached to parent Thread.sleep(1000); assertSame(view.getParent(), mGridView); runTestOnUiThread(new Runnable() { @Override public void run() { // refocus to the removed item and do a focus search. view.requestFocus(); view.focusSearch(View.FOCUS_UP); } }); }
From source file:android.support.v17.leanback.widget.GridWidgetTest.java
public void testFocusFinder() throws Throwable { mInstrumentation = getInstrumentation(); Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class); intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.vertical_linear_with_button); intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 3); intent.putExtra(GridActivity.EXTRA_STAGGERED, false); initActivity(intent);/*from w w w. j av a 2 s. c o m*/ mOrientation = BaseGridView.VERTICAL; mNumRows = 1; // test focus from button to vertical grid view final View button = mActivity.findViewById(R.id.button); assertTrue(button.isFocused()); sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); assertFalse(mGridView.isFocused()); assertTrue(mGridView.hasFocus()); // FocusFinder should find last focused(2nd) item on DPAD_DOWN final View secondChild = mGridView.getChildAt(1); runTestOnUiThread(new Runnable() { @Override public void run() { secondChild.requestFocus(); button.requestFocus(); } }); assertTrue(button.isFocused()); sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); assertTrue(secondChild.isFocused()); // Bug 26918143 Even VerticalGridView is not focusable, FocusFinder should find last focused // (2nd) item on DPAD_DOWN. runTestOnUiThread(new Runnable() { @Override public void run() { button.requestFocus(); } }); mGridView.setFocusable(false); mGridView.setFocusableInTouchMode(false); assertTrue(button.isFocused()); sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); assertTrue(secondChild.isFocused()); }
From source file:com.coincollection.ReorderCollections.java
@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // If the screen rotated and our view got destroyed/recreated, // grab the list of collections from the old view (that we stored // in the bundle.) if (savedInstanceState != null) { mItems = savedInstanceState.getParcelableArrayList("mItems"); mUnsavedChanges = savedInstanceState.getBoolean("mUnsavedChanges"); }// ww w.j ava2 s .c o m RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.reorder_collections_recycler_view); mRecyclerView.setBackgroundColor(Color.BLACK); // Indicate that the contents do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // Use a linear layout manager RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(mLayoutManager); // Set up the adapter that provides the collection entries ReorderAdapter mAdapter = new ReorderAdapter(mItems, this); mRecyclerView.setAdapter(mAdapter); // Register the ItemTouchHelper Callback so that we can allow reordering // collections when the user drags the coin images or long presses and then // drags. ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mAdapter); mItemTouchHelper = new ItemTouchHelper(callback); mItemTouchHelper.attachToRecyclerView(mRecyclerView); // Register a callback so we can know when the list has been reordered mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { @Override public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { super.onItemRangeMoved(fromPosition, toPosition, itemCount); Log.d("onItemRangeMoved", String.valueOf(fromPosition) + " " + String.valueOf(toPosition) + " " + String.valueOf(itemCount)); ReorderCollections fragment = (ReorderCollections) getFragmentManager() .findFragmentByTag("ReorderFragment"); fragment.setUnsavedChanges(true); fragment.showUnsavedTextView(); } }); if (mUnsavedChanges) { showUnsavedTextView(); } //http://stackoverflow.com/questions/7992216/android-fragment-handle-back-button-press view.setFocusableInTouchMode(true); view.requestFocus(); view.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) { if (mUnsavedChanges) { showUnsavedChangesMessage(); } else { closeFragment(); } return true; } return false; } }); }
From source file:administrator.example.com.myscrollview.VerticalViewPager.java
/** * Pagekeypad?/*from www .ja v a2s .c om*/ * @param direction ? * @return handled */ public boolean arrowScroll(int direction) { View currentFocused = findFocus(); if (currentFocused == this) currentFocused = null; boolean handled = false; View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction); if (nextFocused != null && nextFocused != currentFocused) { if (direction == View.FOCUS_UP) { // If there is nothing to the left, or this is causing us to // jump to the down, then what we really want to do is page up. if (currentFocused != null && nextFocused.getTop() >= currentFocused.getTop()) { handled = pageUp(); } else { handled = nextFocused.requestFocus(); } /* end of if */ } else if (direction == View.FOCUS_DOWN) { // If there is nothing to the right, or this is causing us to // jump to the left, then what we really want to do is page right. if (currentFocused != null && nextFocused.getTop() <= currentFocused.getTop()) { handled = pageDown(); } else { handled = nextFocused.requestFocus(); } /* end of if */ } /* end of if */ } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) { // Trying to move left and nothing there; try to page. handled = pageUp(); } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) { // Trying to move right and nothing there; try to page. handled = pageDown(); } /* end of if */ if (handled) { playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction)); } /* end of if */ return handled; }
From source file:com.aliyun.homeshell.Folder.java
public void setFocusOnFirstChild() { View firstChild = mContent.getChildAt(0, 0); if (firstChild != null) { firstChild.requestFocus(); }// w w w . j av a 2 s. co m }
From source file:com.seekon.yougouhui.activity.shop.RegisterShopActivity.java
private void registerShop(final MenuItem item) { nameView.setError(null);/* w w w . j a v a2s .co m*/ addrView.setError(null); descView.setError(null); pwdView.setError(null); pwdConfView.setError(null); userNameView.setError(null); View focusView = null; boolean cancel = false; String requiredError = getString(R.string.error_field_required); final String password = pwdView.getText().toString(); final String passwordConf = pwdConfView.getText().toString(); if (TextUtils.isEmpty(passwordConf)) { pwdConfView.setError(requiredError); focusView = pwdConfView; cancel = true; } else if (passwordConf.length() < 4) { pwdConfView.setError(getString(R.string.error_invalid_password)); focusView = pwdConfView; cancel = true; } if (!password.equals(passwordConf)) { pwdConfView.setError(getString(R.string.error_password_inconfirmed)); focusView = pwdConfView; cancel = true; } if (TextUtils.isEmpty(password)) { pwdView.setError(requiredError); focusView = pwdView; cancel = true; } else if (password.length() < 4) { pwdView.setError(getString(R.string.error_invalid_password)); focusView = pwdView; cancel = true; } final String userPhone = userPhoneView.getText().toString(); if (TextUtils.isEmpty(userPhone)) { userPhoneView.setError(requiredError); focusView = userPhoneView; cancel = true; } final String userName = userNameView.getText().toString(); if (TextUtils.isEmpty(userName)) { userNameView.setError(requiredError); focusView = userNameView; cancel = true; } final String desc = descView.getText().toString(); if (TextUtils.isEmpty(desc)) { descView.setError(requiredError); focusView = descView; cancel = true; } final String address = addrView.getText().toString(); if (TextUtils.isEmpty(address)) { addrView.setError(requiredError); focusView = addrView; cancel = true; } final String name = nameView.getText().toString(); if (TextUtils.isEmpty(name)) { nameView.setError(requiredError); focusView = nameView; cancel = true; } if (cancel) { focusView.requestFocus(); if (focusView.equals(pwdView) || focusView.equals(pwdConfView)) { viewPager.setCurrentItem(2); } else { viewPager.setCurrentItem(1); } return; } if (shopImage == null) { ViewUtils.showToast("."); viewPager.setCurrentItem(1); return; } //TODO:?? // if (busiLicense == null) { // ViewUtils.showToast("?."); // viewPager.setCurrentItem(1); // return; // } if (selectedTrades == null || selectedTrades.isEmpty()) { ViewUtils.showToast("?."); viewPager.setCurrentItem(1); return; } item.setEnabled(false); RestUtils.executeAsyncRestTask(this, new AbstractRestTaskCallback<JSONObjResource>(".") { @Override public RestMethodResult<JSONObjResource> doInBackground() { ShopEntity shop = new ShopEntity(); shop.setAddress(address); shop.setName(name); shop.setDesc(desc); shop.setShopImage(shopImage); shop.setBusiLicense(busiLicense); shop.setTrades(selectedTrades); shop.setLocation(locationEntity); UserEntity currentUser = RunEnv.getInstance().getUser(); if (UserUtils.isAnonymousUser()) { UserEntity emp = new UserEntity(); emp.setPhone(userPhone); emp.setName(userName); emp.setPwd(password); shop.addEmployee(emp); } else { shop.setOwner(currentUser.getUuid()); UserEntity emp = currentUser.clone(); if (emp != null) { emp.setPwd(password); shop.addEmployee(emp); } } return ShopProcessor.getInstance(RegisterShopActivity.this).registerShop(shop); } @Override public void onSuccess(RestMethodResult<JSONObjResource> result) { Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); } @Override public void onFailed(String errorMessage) { onCancelled(); super.onFailed(errorMessage); } @Override public void onCancelled() { item.setEnabled(true); } }); }
From source file:com.example.view.VerticalViewPager.java
/** * Page keypad/*ww w .j ava 2 s .c om*/ * * @param direction * @return handled */ public boolean arrowScroll(int direction) { View currentFocused = findFocus(); if (currentFocused == this) currentFocused = null; boolean handled = false; View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction); if (nextFocused != null && nextFocused != currentFocused) { if (direction == View.FOCUS_UP) { // If there is nothing to the left, or this is causing us to // jump to the down, then what we really want to do is page up. if (currentFocused != null && nextFocused.getTop() >= currentFocused.getTop()) { handled = pageUp(); } else { handled = nextFocused.requestFocus(); } /* end of if */ } else if (direction == View.FOCUS_DOWN) { // If there is nothing to the right, or this is causing us to // jump to the left, then what we really want to do is page // right. if (currentFocused != null && nextFocused.getTop() <= currentFocused.getTop()) { handled = pageDown(); } else { handled = nextFocused.requestFocus(); } /* end of if */ } /* end of if */ } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) { // Trying to move left and nothing there; try to page. handled = pageUp(); } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) { // Trying to move right and nothing there; try to page. handled = pageDown(); } /* end of if */ if (handled) { playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction)); } /* end of if */ return handled; }
From source file:org.woltage.irssiconnectbot.ConsoleActivity.java
/** * Show any prompts requested by the currently visible {@link TerminalView}. *//* w w w .j a va2 s . c o m*/ protected void updatePromptVisible() { // check if our currently-visible terminalbridge is requesting any // prompt services View view = findCurrentView(R.id.console_flip); // Hide all the prompts in case a prompt request was canceled hideAllPrompts(); if (!(view instanceof TerminalView)) { // we dont have an active view, so hide any prompts return; } PromptHelper prompt = ((TerminalView) view).bridge.promptHelper; if (String.class.equals(prompt.promptRequested)) { stringPromptGroup.setVisibility(View.VISIBLE); String instructions = prompt.promptInstructions; boolean password = prompt.passwordRequested; if (instructions != null && instructions.length() > 0) { stringPromptInstructions.setVisibility(View.VISIBLE); stringPromptInstructions.setText(instructions); } else stringPromptInstructions.setVisibility(View.GONE); if (password) { stringPrompt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); stringPrompt.setTransformationMethod(PasswordTransformationMethod.getInstance()); } else { stringPrompt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); stringPrompt.setTransformationMethod(SingleLineTransformationMethod.getInstance()); } stringPrompt.setText(""); stringPrompt.setHint(prompt.promptHint); stringPrompt.requestFocus(); } else if (Boolean.class.equals(prompt.promptRequested)) { booleanPromptGroup.setVisibility(View.VISIBLE); booleanPrompt.setText(prompt.promptHint); booleanYes.requestFocus(); } else { hideAllPrompts(); view.requestFocus(); } }
From source file:com.android.launcher3.folder.Folder.java
public void updateTextViewFocus() { final View firstChild = mContent.getFirstItem(); final View lastChild = mContent.getLastItem(); if (firstChild != null && lastChild != null) { mFolderName.setNextFocusDownId(lastChild.getId()); mFolderName.setNextFocusRightId(lastChild.getId()); mFolderName.setNextFocusLeftId(lastChild.getId()); mFolderName.setNextFocusUpId(lastChild.getId()); // Hitting TAB from the folder name wraps around to the first item on the current // folder page, and hitting SHIFT+TAB from that item wraps back to the folder name. mFolderName.setNextFocusForwardId(firstChild.getId()); // When clicking off the folder when editing the name, this Folder gains focus. When // pressing an arrow key from that state, give the focus to the first item. this.setNextFocusDownId(firstChild.getId()); this.setNextFocusRightId(firstChild.getId()); this.setNextFocusLeftId(firstChild.getId()); this.setNextFocusUpId(firstChild.getId()); // When pressing shift+tab in the above state, give the focus to the last item. setOnKeyListener(new OnKeyListener() { @Override//from w w w .j a v a 2s . c om public boolean onKey(View v, int keyCode, KeyEvent event) { boolean isShiftPlusTab = keyCode == KeyEvent.KEYCODE_TAB && event.hasModifiers(KeyEvent.META_SHIFT_ON); if (isShiftPlusTab && Folder.this.isFocused()) { return lastChild.requestFocus(); } return false; } }); } }
From source file:de.uni_koblenz_landau.apow.ObsDetailFragment.java
/** * Checks input and saves settings to database. *//* w w w. j av a2 s . co m*/ public void save() { // Abort if task is already running. if (mObsUpdateTask != null) { return; } // Reset errors. mQuestionTextView.setError(null); mTestValueView.setError(null); mFindingValueCodedView.setError(null); mSymptomValueView.setError(null); // Store UI values. mQuestionText = mQuestionTextView.getText().toString().trim(); mTestValue = mTestValueView.getText().toString().trim(); // Find observation type. int type = -1; // Observation is question. if ((mObs.getConcept() != null && mObs.getConcept().getConceptId() == Constants.QUESTION_CONCEPT_ID) || (mObs.getConcept() == null && mClassSpinner.getSelectedItemPosition() == Constants.CLASS_QUESTION)) { type = KEY_QUESTION; // Observation is test. } else if ((mObs.getConcept() != null && mObs.getConcept().getConceptClass() != null && mObs.getConcept().getConceptClass().getConceptClassId() == Constants.TEST_CLASS_ID) || (mObs.getConcept() == null && mClassSpinner.getSelectedItemPosition() == Constants.CLASS_TEST)) { type = KEY_TEST; // Observation is finding. } else if ((mObs.getConcept() != null && mObs.getConcept().getConceptId() == Constants.FINDING_SET_CONCEPT_ID) || (mObs.getConcept() == null && mClassSpinner.getSelectedItemPosition() == Constants.CLASS_FINDING)) { type = KEY_FINDING; // Observation is symptom. } else if ((mObs.getConcept() != null && mObs.getConcept().getConceptClass() != null && mObs.getConcept().getConceptClass().getConceptClassId() == Constants.SYMPTOM_CONCEPT_CLASS_ID) || (mObs.getConcept() == null && mClassSpinner.getSelectedItemPosition() == Constants.CLASS_SYMPTOM)) { type = KEY_SYMPTOM; } Boolean cancel = false; View focusView = null; // Validate UI input. Double mTestValueDouble = null; if (type == KEY_QUESTION) { if (TextUtils.isEmpty(mQuestionText)) { mQuestionTextView.setError(getString(R.string.error_field_required)); focusView = mQuestionTextView; cancel = true; } } else if (type == KEY_TEST) { if (TextUtils.isEmpty(mTestValue)) { mTestValueView.setError(getString(R.string.error_field_required)); focusView = mTestValueView; cancel = true; } else { try { mTestValueDouble = Double.parseDouble(mTestValue); Double low = ((ListViewItem) mTestConceptView.getSelectedItem()).getValue1(); Double hi = ((ListViewItem) mTestConceptView.getSelectedItem()).getValue2(); if (low != -1 && hi != -1 && (mTestValueDouble < low || mTestValueDouble > hi)) { mTestValueView.setError(getActivity().getString(R.string.error_field_out_of_range) + " [" + low + "," + hi + "]"); focusView = mTestValueView; cancel = true; } } catch (NumberFormatException nfe) { mTestValueView.setError(getActivity().getString(R.string.error_field_not_numeric)); focusView = mTestValueView; cancel = true; } } } else if (type == KEY_FINDING) { if (findingCode == -1) { mFindingValueCodedView.setError(getActivity().getString(R.string.error_field_not_from_list)); focusView = mTestValueView; cancel = true; } } else if (type == KEY_SYMPTOM) { if (symptomCode == -1) { mSymptomValueView.setError(getActivity().getString(R.string.error_field_not_from_list)); focusView = mTestValueView; cancel = true; } } // If there are errors cancel, else save to database. if (cancel) { focusView.requestFocus(); } else { // Update obs object if (type == KEY_QUESTION) { mObs.setConcept(new Concept(Constants.QUESTION_CONCEPT_ID)); mObs.setValueText(mQuestionText); } else if (type == KEY_TEST) { ConceptNumeric concept = new ConceptNumeric( ((ListViewItem) mTestConceptView.getSelectedItem()).getId()); ConceptDatatype datatype = new ConceptDatatype(); datatype.setName(Constants.CONCEPT_DATATYPE_NUMERIC); concept.setDatatype(datatype); mObs.setConcept(concept); mObs.setValueNumeric(mTestValueDouble); } else if (type == KEY_FINDING) { if (mObs.hasGroupMembers()) { for (Obs obs : mObs.getGroupMembers()) { if (obs.getConcept().getConceptId() == Constants.FINDING_CONCEPT) { if (findingCode != -1) { obs.setValueCoded(new Concept(findingCode)); } } if (obs.getConcept().getConceptId() == Constants.FINDING_CERTAINTY) { if (mFindingCertaintyView.isChecked()) { obs.setValueCoded(new Concept(Constants.FINDING_CERTAINTY_CONFIRMED)); } else { obs.setValueCoded(new Concept(Constants.FINDING_CERTAINTY_NOT_CONFIRMED)); } } if (obs.getConcept().getConceptId() == Constants.FINDING_SEVERITY) { if (mFindingSeverityCodedView.isChecked()) { obs.setValueCoded(new Concept(Constants.FINDING_SEVERITY_PRIMARY)); } else { obs.setValueCoded(new Concept(Constants.FINDING_SEVERITY_SECONDARY)); } } } } else { mObs.setConcept(new Concept(Constants.FINDING_SET_CONCEPT_ID)); Obs concept = new Obs(); concept.setConcept(new Concept(Constants.FINDING_CONCEPT)); concept.setValueCoded(new Concept(findingCode)); mObs.addGroupMember(concept); Obs certainty = new Obs(); certainty.setConcept(new Concept(Constants.FINDING_CERTAINTY)); if (mFindingCertaintyView.isChecked()) { certainty.setValueCoded(new Concept(Constants.FINDING_CERTAINTY_CONFIRMED)); } else { certainty.setValueCoded(new Concept(Constants.FINDING_CERTAINTY_NOT_CONFIRMED)); } mObs.addGroupMember(certainty); Obs severity = new Obs(); severity.setConcept(new Concept(Constants.FINDING_SEVERITY)); if (mFindingSeverityCodedView.isChecked()) { severity.setValueCoded(new Concept(Constants.FINDING_SEVERITY_PRIMARY)); } else { severity.setValueCoded(new Concept(Constants.FINDING_SEVERITY_SECONDARY)); } mObs.addGroupMember(severity); } } else if (type == KEY_SYMPTOM) { mObs.setConcept(new Concept(symptomCode)); } // Start update task. mObsUpdateTask = new ObsUpdateTask(); TaskActivityReference.getInstance().addTask(SettingsUpdateTask.TASK_ID, mObsUpdateTask, this); mObsUpdateTask.execute(mObs); } }