安卓的EditText焦点跳转到另一个里面滚动型 [英] Android: EditText focus jumps to another inside ScrollView

查看:266
本文介绍了安卓的EditText焦点跳转到另一个里面滚动型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ViewPager多个片段水平相邻。每个片段都有一个包含一个表有许多的EditText视图的滚动型。当我点击一个EditText上,它获得焦点,然后失去了一些其他的EditText在最左边的片段。有时,对焦开关立即发生,有时候当我打字成的EditText。一般,最上面的EditText在最左边的片段窃取焦点

我看不到问题在最左边的片段的EditText被点击时,即使它是不是最上面。它像Android不喜欢专注于一个TextView不是沿左边缘。这是否听起来很熟悉?

我已经试过失败覆盖,滚动型的findFocus()方法,在<一个建议的跳跃href="http://stackoverflow.com/questions/6679294/focus-randomly-jumps-when-scrolling">randomly-jumping.附源$ C ​​$ c是巨大的,但它可能是有用的人......

 包com.example.slideViewPager;

进口的java.util.ArrayList;

进口com.example.slideViewPager.R;

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

公共类SlideViewPager扩展FragmentActivity {
    私有静态上下文g;
    私有静态的ArrayList&LT;字符串&GT;页;
    私有静态诠释currentItem;后倾//恢复页面位置
    私有静态诠释[] scrollStateY;
    私有静态NotesViewFragment [] fragmentArray;
    私有静态诠释ID; //创建独特的见解IDS

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        G = getApplicationContext();
        的setContentView(R.layout.activity_slide_view_pager);
        页=新的ArrayList&LT;字符串&GT;();
        对(INT I = 0; I&4; ++ⅰ)
            pages.add(Integer.toString(ⅰ));

        如果(savedInstanceState == NULL){
            currentItem = 0;
            scrollStateY =新INT [pages.size()]; //假设全0
            的id = 0;
        }
        其他 {
            currentItem = savedInstanceState.getInt(currentItem);
            scrollStateY = savedInstanceState.getIntArray(scrollStateY);
            ID = savedInstanceState.getInt(ID);
            的System.out.println(注释的onCreate RESTORE:currentItem =+ currentItem +; ID =+ ID);
        }

        fragmentArray =新NotesViewFragment [pages.size()];
        的System.out.println(的onCreate:启动);
        super.onCreate(savedInstanceState);
        ViewPager viewPager =(ViewPager)findViewById(R.id.viewpager);
        NotesPagerAdapter pagerAdapter =新NotesPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(currentItem);
        viewPager.setOnPageChangeListener(onPageChangeListener);
        的System.out.println(的onCreate:做);
    }

    @覆盖
    公共无效的onSaveInstanceState(束捆){
        的System.out.println(注:的onSaveInstanceState:currentItem =+ currentItem);
        super.onSaveInstanceState(包);
        bundle.putInt(currentItem,currentItem);
        bundle.putIntArray(scrollStateY,scrollStateY);
        bundle.putInt(身份证,身份证);
    }

    公共无效onBack pressed(){
        的System.out.println(注:onBack pressed:currentItem =+ currentItem);
        super.onBack pressed();
    }

    ////////////////////////////////////////////////// ///////////////
    公共静态类NotesPagerAdapter扩展FragmentPagerAdapter {

        公共NotesPagerAdapter(FragmentManager FM){
            超(FM);
            的System.out.println(NotesPagerAdapter:此=+本);
        }

        @覆盖
        公共片段的getItem(INT位置){
            字符串页次= pages.get(位置);
            NotesViewFragment notesViewFragment = NotesViewFragment.initialize(页次,位置); //创建新的片段
            的System.out.println(NotesPagerAdapter:的getItem:设立的职位:+位置+=页:+页次+片段=+ notesViewFragment);
            返回notesViewFragment;
        }

        @覆盖
        公众诠释getCount将(){
            返回pages.size();
        }

        //没有被这里完成
        @覆盖
        公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
            的System.out.println(NotesPagerAdapter:destroyItem位置=+位置);
            super.destroyItem(集装箱,位置,对象);
        }

        @覆盖
        公众持股量getPageWidth(INT位置){
            的System.out.println(NotesPagerAdapter:getPageWidth位置=+位置);
            返程(1.0F / 2); //改变#页/视图
        }
    }

    ////////////////////////////////////////////////// ////
    公共静态类NotesViewFragment扩展片段{
        私人诠释的位置; //需要保存scrollY到scrollStateY []
        私人字符串页次; // 指数
        私人滚动型itemScrollView;

        公共NotesViewFragment(){
            超();
            的System.out.println(NotesViewFragment:新的:+本);
        }

        公共静态NotesViewFragment初始化(字符串页面,INT位置){
            NotesViewFragment notesViewFragment =新NotesViewFragment();
            的System.out.println(NotesViewFragment:初始化:这一点:+ notesViewFragment +;页面:+网页+,位置=+位置);
            捆绑的args =新包();
            args.putString(页次,页);
            args.putInt(位置,位置);
            scrollStateY [位置] = 0; //启动顶部
            notesViewFragment.setArguments(参数);
            返回notesViewFragment;
        }

        @覆盖
        公共无效的onSaveInstanceState(包outState){
            super.onSaveInstanceState(outState);
            的System.out.println(NotesViewFragment:此的onSaveInstanceState:+本+;页面:+页次+;滚动视图=+ itemScrollView);
            // itemScrollView将是无效偶尔(附而不是createViewed)
            如果(itemScrollView!= NULL)
                scrollStateY [位置] = itemScrollView.getScrollY();
            outState.putString(页次,页次);
        }

        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            捆绑的args = getArguments();
            如果(参数!= NULL){
                页次= args.getString(页次);
                位置= args.getInt(位置);
                fragmentArray [位置] =这一点;
                的System.out.println(NotesViewFragment:的onCreate:这样的:+本+;页面:+页次+,位置=+位置+,yPos =+ scrollStateY [位置]);
            }
            否则//不应该发生
                的System.out.println(NotesViewFragment:的onCreate:无参数!!!这样的:+本);
        }

        公共无效onDestroyView(){
            super.onDestroyView();
            如果(itemScrollView!= NULL)//可能为空,如果不createViewed
                scrollStateY [位置] = itemScrollView.getScrollY();

            的System.out.println(NotesViewFragment:onDestroyView:+页次+这个=+本+; scrollY =+ scrollStateY [位置]);
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
            如果(savedInstanceState!= NULL){//配置变化发生
                页次= savedInstanceState.getString(页次);
                的System.out.println(NotesViewFragment:onCreateView:RESTORED+页次+这个=+本+; yPos =+ scrollStateY [位置]);
            }
            其他
                的System.out.println(NotesViewFragment:onCreateView:+页次+,它=+本);

            //充气含标题和正文布局。
            ViewGroup中rootView =(ViewGroup中)inflater.inflate(R.layout.fragment_screen_slide_page,集装箱,假);
            TableLayout itemNotesTable =(TableLayout)rootView.findViewById(R.id.tableLayoutNotes);
            TextView的itemTitle =(TextView中)rootView.findViewById(R.id.textViewTitle);
            itemScrollView =(滚动型)rootView.findViewById(R.id.scroll_form);
            itemTitle.setText(团队+页次);

            的for(int i = 0; I&LT; 12; ++ I){
                的TableRow TR =新的TableRow(G);
                tr.setId(++ ID);

                ImageView的itemThumbnail =新ImageView的(G);
                itemThumbnail.setId(++ ID);
                itemThumbnail.setImageResource(R.drawable.notes); //设置图标
                tr.addView(itemThumbnail);

                的LinearLayout LL =新的LinearLayout(G);
                ll.setId(++ ID);
                ll.setOrientation(LinearLayout.VERTICAL);
                字符串文本=团队+页次;
                ll.addView(makeTextItem(文字,FALSE));

                的TextView TextView的;
                如果((ID%2)== 0){//做一些条目编辑
                    TextView的=新的EditText(G);
                    textView.setFocusable(真正的);
                    textView.setFocusableInTouchMode(真正的);
                    textView.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
                    textView.setOnFocusChangeListener(onFocusChangeListener);
                }
                其他
                    TextView的=新的TextView(G);

                textView.setId(++ ID);
                字符串str =行+ I +是等等等等等等等等等等等等等等;
                textView.setText(STR);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(16);
                textView.setPadding(2,2,2,2); //开始,顶部,结束,底部
                ll.addView(TextView的);
                tr.addView(Ⅱ);
                itemNotesTable.addView(TR);
                tr.setOnClickListener(noteClicked); //使一切都在表行点击
            }

            如果(scrollStateY [位置]!= 0){
                itemScrollView.post(新的Runnable(){
                    公共无效的run(){
                        itemScrollView.scrollTo(0,scrollStateY [位置]);
                    }
                });
            }
            返回rootView;
        }

        //从fillNotes称为
        私有静态的TextView makeTextItem(字符串标签,布尔粗体){
            TextView的项目=新的TextView(G);
            item.setId(++ ID);
            item.setText(标签);
            item.setTextColor(Color.BLACK);
            item.setTextSize(16);
            如果(粗体)
                item.setTypeface(NULL,Typeface.BOLD);
            归还物品;
        }
    }

    //监听水平页面滚动
    ViewPager.OnPageChangeListener onPageChangeListener =新ViewPager.SimpleOnPageChangeListener(){
        @覆盖
        公共无效onPageSelected(INT位置){
            的System.out.println(onPageChangeListener:离开页面:+ currentItem +;对于页:+位置);
            currentItem =位置;
        }
    };

    静态View.OnFocusChangeListener onFocusChangeListener =新View.OnFocusChangeListener(){

        @覆盖
        公共无效onFocusChange(查看视图,布尔hasFocus){
            的System.out.println(onFocusChangeListener:+ hasFocus +,+视图);
            如果(!hasFocus){
                EditText上EDITTEXT =(EditText上)查看;
                字符串文本= editText.getText()的toString()。
                的System.out.println(onFocusChangeListener:+文字);
            }
        }
    };

    静态OnClickListener noteClicked =新OnClickListener(){
        公共无效的onClick(查看TR){
            对于(查看父=潮流;!家长= NULL;父=(查看)parent.getParent()){
                对于(NotesViewFragment片段:fragmentArray)
                    如果(片段= NULL和放大器;!&安培; fragment.itemScrollView ==父){
                        的System.out.println(noteClicked:页:+ fragment.pageNum);
                        返回;
                    }
            }
        }
    };
}
 

有几个XML文件:fragment_screen_slide_page.xml ..........

 &LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /内容
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;

    &LT;的TextView
        机器人:ID =@ + ID / textViewTitle
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:文本=
        机器人:textAppearance =:/&gt;中的Andr​​oid ATTR / textAppearanceLarge?

    &LT;查看
        机器人:ID =@ + ID /分离器
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =1dip
        机器人:layout_alignParentRight =真
        机器人:layout_marginTop =4DP
        机器人:layout_below =@ + ID / textViewTitle
        机器人:后台=#888888/&GT;

    &LT;滚动型
        机器人:ID =@ + ID / scroll_form
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_below =@ + ID /分隔符&GT;

        &LT; TableLayout
            机器人:ID =@ + ID / tableLayoutNotes
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:stretchColumns =1&GT;
        &LT; / TableLayout&GT;
    &LT; /滚动型&GT;
&LT; / RelativeLayout的&GT;
 

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

 &LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent

    机器人:descendantFocusability =beforeDescendants
    机器人:focusableInTouchMode =真
    工具:上下文=页面&GT;

    &LT; android.support.v4.view.ViewPager
        机器人:ID =@ + ID / viewpager
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/&GT;
&LT; / RelativeLayout的&GT;
 

解决方案

对焦转换到最左边的片段,因为这片段是 ViewPager 的当前项目。您可以尝试使用 setCurrentItem(项目)当前项设置为另一个片段(当用户点击就可以了),所以左一不偷焦点。

这是在填充的结束,因为发生的事情() ViewPager 它总是能给对焦功能以一种观点认为,在当前的项目。您也可以通过复制 ViewPager 源和改变这个code,使这是对任何片段解决这个问题 屏幕上有一个孩子与重点。

更改此:

 如果(hasFocus()){
    查看currentFocused = findFocus();
    ItemInfo II = currentFocused!= NULL? infoForAnyChild(currentFocused):空;
    如果(二== NULL || ii.position!= mCurItem){
        的for(int i = 0; I&LT; getChildCount();我++){
            查看孩子= getChildAt(我);
            II = infoForChild(子);
            如果(二= NULL和放大器;!&安培; ii.position == mCurItem){
                如果(child.requestFocus(FOCUS_FORWARD)){
                    打破;
                }
            }
        }
    }
}
 

要这样的:

 如果(hasFocus()){
    查看currentFocused = findFocus();
    ItemInfo II = currentFocused!= NULL? infoForAnyChild(currentFocused):空;
    如果(二== NULL ||!infoIsOnScreen(二)){
        的for(int i = 0; I&LT; getChildCount();我++){
            查看孩子= getChildAt(我);
            II = infoForChild(子);
            如果(ⅱ=空&安培;!&安培; infoIsOnScreen(ⅱ)){
                如果(child.requestFocus(FOCUS_FORWARD)){
                    打破;
                }
            }
        }
    }
}

公共布尔infoIsOnScreen(ItemInfo资讯){
    // code,以确定是否认为是使用屏幕ii.offset等上
}
 

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.

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?

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;
                    }
            }
        }
    };
}

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>

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>

解决方案

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.

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.

Change this:

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;
                }
            }
        }
    }
}

To something like this:

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.
}

这篇关于安卓的EditText焦点跳转到另一个里面滚动型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆