久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Android:EditText焦點(diǎn)跳轉(zhuǎn)到ScrollView中的另一個(gè)

Android: EditText focus jumps to another inside ScrollView(Android:EditText焦點(diǎn)跳轉(zhuǎn)到ScrollView中的另一個(gè))
本文介紹了Android:EditText焦點(diǎn)跳轉(zhuǎn)到ScrollView中的另一個(gè)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我有多個(gè)使用 ViewPager 水平相鄰的片段.每個(gè)片段都有一個(gè) ScrollView,其中包含一個(gè)帶有許多 EditText 視圖的表.當(dāng)我單擊 EditText 時(shí),它獲得焦點(diǎn),然后將其丟失給最左側(cè)片段中的其他一些 EditText.有時(shí)焦點(diǎn)切換會(huì)立即發(fā)生,有時(shí)在我輸入 EditText 時(shí)會(huì)發(fā)生.通常,最左側(cè)片段中最頂部的 EditText 會(huì)搶占焦點(diǎn).

I have multiple fragments horizontally adjacent using a ViewPager. Each fragment has a ScrollView that contains a table with many EditText views. When I click on a EditText, it gains focus and then loses it to some other EditText in the leftmost fragment. Sometimes the focus switch happens immediately, sometimes while I am typing into a EditText. Usually, the topmost EditText in the leftmost fragment steals focus.

單擊最左側(cè)片段中的 EditText 時(shí),我看不到問(wèn)題,即使它不是最頂部的片段.它就像 Android 不喜歡關(guān)注不在左邊緣的 TextView.這聽(tīng)起來(lái)很熟悉嗎?

I don't see the problem when a EditText in the leftmost fragment is clicked, even if it is not the topmost one. Its like Android doesn't like focusing on a TextView that is not along the left edge. Does this sound familiar?

按照 Skip 在 randomly- 中的建議,我嘗試覆蓋 ScrollView 中的 findFocus() 方法,但未成功跳躍.附上的源代碼很大,但可能對(duì)其他人有用...

I have unsuccessfully tried overriding the findFocus() method in ScrollView as suggested by Skip in randomly-jumping. The source code attached is huge, but it may be useful to others...

package com.example.slideViewPager;

import java.util.ArrayList;

import com.example.slideViewPager.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SlideViewPager extends FragmentActivity {
    private static Context g;
    private static ArrayList<String> pages;
    private static int currentItem;    // restore page position after tilt
    private static int[] scrollStateY;
    private static NotesViewFragment[] fragmentArray;
    private static int id;            // to create unique view ids

    @Override
    public void onCreate(Bundle savedInstanceState) {
        g = getApplicationContext();
        setContentView(R.layout.activity_slide_view_pager);
        pages = new ArrayList<String>();
        for (int i = 0; i < 4; ++i)
            pages.add(Integer.toString(i));

        if (savedInstanceState == null) {
            currentItem = 0;
            scrollStateY = new int[pages.size()];    // assume all 0s
            id = 0;
        }
        else {
            currentItem = savedInstanceState.getInt("currentItem");
            scrollStateY = savedInstanceState.getIntArray("scrollStateY");
            id = savedInstanceState.getInt("id");
            System.out.println("Notes onCreate RESTORE: currentItem="+currentItem+"; id="+id);
        }

        fragmentArray = new NotesViewFragment[pages.size()];
        System.out.println("onCreate: start");
        super.onCreate(savedInstanceState);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        NotesPagerAdapter pagerAdapter = new NotesPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(currentItem);
        viewPager.setOnPageChangeListener(onPageChangeListener);
        System.out.println("onCreate: done");
    }

    @Override
    public void onSaveInstanceState(Bundle bundle) {
        System.out.println("Notes: onSaveInstanceState: currentItem="+currentItem);
        super.onSaveInstanceState(bundle);
        bundle.putInt("currentItem", currentItem);
        bundle.putIntArray("scrollStateY", scrollStateY);
        bundle.putInt("id", id);
    }

    public void onBackPressed() {
        System.out.println("Notes: onBackPressed: currentItem="+currentItem);
        super.onBackPressed();
    }

    /////////////////////////////////////////////////////////////////
    public static class NotesPagerAdapter extends FragmentPagerAdapter {

        public NotesPagerAdapter(FragmentManager fm) {
            super(fm);
            System.out.println("NotesPagerAdapter: this="+this);
        }

        @Override
        public Fragment getItem(int position) {
            String pageNum = pages.get(position);
            NotesViewFragment notesViewFragment = NotesViewFragment.initialize(pageNum, position);        // create new fragment
            System.out.println("NotesPagerAdapter: getItem: CREATED position:"+position+" = page:"+pageNum+"; fragment="+notesViewFragment);
            return notesViewFragment;
        }

        @Override
        public int getCount() {
            return pages.size();
        }

        // nothing being done here
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            System.out.println("NotesPagerAdapter: destroyItem position="+position);
            super.destroyItem(container, position, object);
        }

        @Override
        public float getPageWidth(int position) {
            System.out.println("NotesPagerAdapter: getPageWidth position="+position);
            return (1.0f/2);    // vary # pages/view
        }
    }

    //////////////////////////////////////////////////////
    public static class NotesViewFragment extends Fragment {
        private int position;    // needed to save scrollY into scrollStateY[]
        private String pageNum;    // index
        private ScrollView itemScrollView;

        public NotesViewFragment() {
            super();
            System.out.println("NotesViewFragment: new this:"+this);
        }

        public static NotesViewFragment initialize(String page, int position) {
            NotesViewFragment notesViewFragment = new NotesViewFragment();
            System.out.println("NotesViewFragment: initialize: this:"+notesViewFragment+"; page: "+page+"; position="+position);
            Bundle args = new Bundle();
            args.putString("pageNum", page);
            args.putInt("position", position);
            scrollStateY[position] = 0;        // start at top
            notesViewFragment.setArguments(args);
            return notesViewFragment;
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            System.out.println("NotesViewFragment: onSaveInstanceState this:"+this+"; page: "+pageNum+"; scrollView="+itemScrollView);
            // itemScrollView will be null occasionally (attached but not createViewed)
            if (itemScrollView != null)
                scrollStateY[position] = itemScrollView.getScrollY();
            outState.putString("pageNum", pageNum);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Bundle args = getArguments();
            if (args != null) {
                pageNum = args.getString("pageNum");
                position = args.getInt("position");
                fragmentArray[position] = this;
                System.out.println("NotesViewFragment: onCreate: this:"+this+"; page: "+pageNum+"; position="+position+"; yPos="+scrollStateY[position]);
            }
            else    // should not happen
                System.out.println("NotesViewFragment: onCreate: NO ARGS !!! this:"+this);            
        }

        public void onDestroyView() {
            super.onDestroyView();
            if (itemScrollView != null)        // could be null if not createViewed
                scrollStateY[position] = itemScrollView.getScrollY();

            System.out.println("NotesViewFragment: onDestroyView: "+pageNum+" this="+this+"; scrollY="+scrollStateY[position]);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            if (savedInstanceState != null) {    // config change happened
                pageNum = savedInstanceState.getString("pageNum");
                System.out.println("NotesViewFragment: onCreateView: RESTORED "+pageNum+" this="+this+"; yPos="+scrollStateY[position]);
            }
            else
                System.out.println("NotesViewFragment: onCreateView: "+pageNum+"; this="+this);

            // Inflate the layout containing a title and body text.
            ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
            TableLayout itemNotesTable = (TableLayout) rootView.findViewById(R.id.tableLayoutNotes);
            TextView itemTitle = (TextView) rootView.findViewById(R.id.textViewTitle);
            itemScrollView = (ScrollView) rootView.findViewById(R.id.scroll_form);
            itemTitle.setText("Team "+pageNum);

            for (int i = 0; i < 12; ++i) {
                TableRow tr = new TableRow(g);
                tr.setId(++id);

                ImageView itemThumbnail = new ImageView(g);
                itemThumbnail.setId(++id);
                itemThumbnail.setImageResource(R.drawable.notes);    // set icon
                tr.addView(itemThumbnail);

                LinearLayout ll = new LinearLayout(g);
                ll.setId(++id);
                ll.setOrientation(LinearLayout.VERTICAL);
                String text = " Team "+pageNum;
                ll.addView(makeTextItem(text, false));

                TextView textView;
                if ((id % 2) == 0) {    // make some entries editable
                    textView = new EditText(g);
                    textView.setFocusable(true);
                    textView.setFocusableInTouchMode(true);
                    textView.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
                    textView.setOnFocusChangeListener(onFocusChangeListener);
                }
                else
                    textView = new TextView(g);

                textView.setId(++id);
                String str = "Line "+i+" is blah blah blah and more blah blah blah";
                textView.setText(str);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(16);
                textView.setPadding(2, 2,  2,  2);        // start, top, end, bottom
                ll.addView(textView);
                tr.addView(ll);
                itemNotesTable.addView(tr);
                tr.setOnClickListener(noteClicked);        // make everything in table row clickable
            }

            if (scrollStateY[position] != 0) {
                itemScrollView.post(new Runnable() {
                    public void run() {
                        itemScrollView.scrollTo(0, scrollStateY[position]);
                    }
                });
            }
            return rootView;
        }

        // called from fillNotes
        private static TextView makeTextItem(String label, boolean bold) {
            TextView item = new TextView(g);
            item.setId(++id);
            item.setText(label);
            item.setTextColor(Color.BLACK);
            item.setTextSize(16);
            if (bold)
                item.setTypeface(null, Typeface.BOLD);
            return item;
        }
    }

    // listens for horizontal page scrolls
    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            System.out.println("onPageChangeListener: leaving page:"+currentItem+"; for page: "+position);
            currentItem = position;
        }
    };

    static View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            System.out.println("onFocusChangeListener: "+hasFocus+"; "+view);
            if (! hasFocus) {
                EditText editText = (EditText) view;
                String text = editText.getText().toString();
                System.out.println("onFocusChangeListener: "+text);
            }
        }
    };

    static OnClickListener noteClicked = new OnClickListener() {
        public void onClick(View tr) {
            for (View parent = tr; parent != null; parent = (View) parent.getParent()) {
                for (NotesViewFragment fragment : fragmentArray)
                    if (fragment != null  &&  fragment.itemScrollView == parent) {
                        System.out.println("noteClicked: page: "+fragment.pageNum);
                        return;
                    }
            }
        }
    };
}

有幾個(gè) XML 文件:fragment_screen_slide_page.xml ..........

There are a couple of XML files: fragment_screen_slide_page.xml ..........

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <View
        android:id="@+id/separator"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:layout_alignParentRight="true"
        android:layout_marginTop="4dp"
        android:layout_below="@+id/textViewTitle"
        android:background="#888888" />

    <ScrollView
        android:id="@+id/scroll_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/separator" >

        <TableLayout
            android:id="@+id/tableLayoutNotes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1"  >
        </TableLayout>
    </ScrollView>
</RelativeLayout>

和activity_slide_view_pager.xml ----------

and activity_slide_view_pager.xml ----------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    tools:context=".Page" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

推薦答案

焦點(diǎn)正在更改到最左邊的片段,因?yàn)樵撈问?ViewPager 的當(dāng)前項(xiàng).您可以嘗試使用 setCurrentItem(item) 將當(dāng)前項(xiàng)目設(shè)置為另一個(gè)片段(當(dāng)用戶單擊它時(shí)),這樣左邊的項(xiàng)目就不會(huì)竊取焦點(diǎn).

Focus is changing to the leftmost fragment because that fragment is the current item of the ViewPager. You might try to use setCurrentItem(item) to set the current item to the other fragment (when the user clicks on it) so the left one doesn't steal focus.

發(fā)生這種情況是因?yàn)樵?ViewPagerpopulate() 函數(shù)結(jié)束時(shí),它總是將焦點(diǎn)放在當(dāng)前項(xiàng)目中的視圖上.您還可以通過(guò)復(fù)制 ViewPager 源并更改此代碼以允許屏幕讓孩子專注.

This is happening because at the end of the populate() function of the ViewPager it will always give focus to a view that is in the current item. You could also fix this issue by copying the ViewPager source and changing this code to allow any fragment which is on the screen to have a child with focus.

改變這個(gè):

if (hasFocus()) {
    View currentFocused = findFocus();
    ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
    if (ii == null || ii.position != mCurItem) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            ii = infoForChild(child);
            if (ii != null && ii.position == mCurItem) {
                if (child.requestFocus(FOCUS_FORWARD)) {
                    break;
                }
            }
        }
    }
}

這樣的:

if (hasFocus()) {
    View currentFocused = findFocus();
    ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
    if (ii == null || !infoIsOnScreen(ii)) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            ii = infoForChild(child);
            if (ii != null && infoIsOnScreen(ii)) {
                if (child.requestFocus(FOCUS_FORWARD)) {
                    break;
                }
            }
        }
    }
}

public boolean infoIsOnScreen(ItemInfo info) {
    // Code to determine if the view is on the screen using ii.offset etc.
}

這篇關(guān)于Android:EditText焦點(diǎn)跳轉(zhuǎn)到ScrollView中的另一個(gè)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開(kāi)的 EditText 中的開(kāi)始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問(wèn)題)
主站蜘蛛池模板: 国产午夜精品一区二区三区嫩草 | 久久国内 | 日本精品视频在线 | 丝袜 亚洲 欧美 日韩 综合 | 亚洲一区二区三区观看 | 国产一级毛片精品完整视频版 | 成人视屏在线观看 | 天天干天天操天天射 | 91视频.| 不卡一区二区在线观看 | 成年人的视频免费观看 | 欧美国产精品一区二区三区 | 免费的一级视频 | 在线午夜 | 精品久久久久久久久久久下田 | 色婷婷综合在线观看 | www国产亚洲精品久久网站 | 中文日韩在线视频 | 日本久久久久久 | 6996成人影院网在线播放 | 美人の美乳で授乳プレイ | 在线免费观看毛片 | 国产精品高潮呻吟久久 | 国产成人午夜电影网 | 99精品一级欧美片免费播放 | 岛国毛片在线观看 | 成人影院av | 精品久久久一区 | 久久精品中文字幕 | 亚洲成人久久久 | 日韩一级黄色毛片 | 国产成人免费视频网站视频社区 | 特黄毛片视频 | 久久天天躁狠狠躁夜夜躁2014 | 特黄色一级毛片 | 天天综合永久入口 | 亚洲精品在线观看视频 | 在线视频中文字幕 | 久久亚洲国产精品 | 亚洲视频中文字幕 | 日韩中文字幕一区二区 |