草庐IT

android - 单击任何 EditText 时屏幕向上滚动

coder 2023-12-28 原文

我有一个屏幕有 4 个 EditText,例如 menuName、DishName、Tag 和 Price。
我需要在点击它时将我的标签 Edittext 设置在顶部(+56dip)。现在它工作正常。

当我点击 Tag EditText 时,它会像我想要的那样向上移动。

我卡在哪里

向上移动布局工作正常,但如果我单击其他 EditText,它也会向上移动,这是我不想要的。我只是不想在单击 Tag EditText 时向上滚动屏幕。

这是我的java代码。

    final View activityRootView = findViewById(R.id.RelativeAdd);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = activityRootView.getRootView()
                            .getHeight() - activityRootView.getHeight();
                    Log.i("TEST",
                            "GLOBAL LAYOUT " + activityRootView.getHeight());
                    Log.i("TEST", "GLOBAL LAYOUT rel"+ relativeLayoutHeight);
                    if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        performScrollingUp();
                        Log.i("TEsT", "Keyboard visible");
} else {
    Log.i("TEsT", "Keyboard not visible");
    performScrollingDown();
    txtDishTags.setDropDownHeight(ViewGroup.LayoutParams.FILL_PARENT);
}
}
});      
     protected void performScrollingUp() {
    if (!isTagUp) {
        txtDishTags.setCursorVisible(true);
        txtDishTags.setFocusable(true);
        txtDishTags.setFocusableInTouchMode(true);
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(txtDishTags, 0);

        firstRelativeLayout.setVisibility(View.GONE);
        secondRelativeLayout.setVisibility(View.GONE);
        firstLinearLayout.setVisibility(View.GONE);
        secondLinearLayout.setVisibility(View.GONE);

        rlp = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                android.app.ActionBar.LayoutParams.WRAP_CONTENT);
        rlp.addRule(RelativeLayout.BELOW, R.id.tempRelativeLayout);
        tagRelativeLayout.setLayoutParams(rlp);
        isTagUp = true;
        final float scale = getResources().getDisplayMetrics().density;
        int width = (int) (60 * scale + 0.5f);
        int height = (int) (44 * scale + 0.5f);
        rlp2 = new RelativeLayout.LayoutParams(width, height);
        rlp2.addRule(RelativeLayout.BELOW, R.id.tempRelativeLayout);
        rlp2.setMargins(24, 0, 0, 0);
        thirdRelativLayout.setLayoutParams(rlp2);
        lbltag.setGravity(Gravity.CENTER);

    }

}

xml文件代码供理解。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeAdd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFEF" 
android:orientation="vertical">

<ViewStub
    android:id="@+id/vsHeader2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inflatedId="@+id/header"
    android:layout="@layout/copyofheader" />

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <RelativeLayout
        android:id="@+id/myRelativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dip"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/hotelName"
                android:layout_width="fill_parent"
                android:layout_height="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:background="@drawable/borderforloginedittext"
                android:ems="10"
                android:hint="Name on menu (optional)"
                android:inputType="textCapWords"
                android:paddingLeft="70dp"
                android:textColor="#333333"
                android:textColorHint="#999999"
                android:textSize="16sp" >
                <requestFocus />
            </EditText>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/firstRelativeLayout"
            android:layout_width="60dip"
            android:layout_height="44dip"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="12dip" >

            <TextView
                android:id="@+id/labelName"
                android:layout_width="60dip"
                android:layout_height="44dip"
                android:gravity="center"
                android:text="NAME"
                android:textColorHint="#999999"
                android:textSize="10sp"
                android:visibility="visible" >
            </TextView>
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/firstRelativeLayout"
            android:layout_marginTop="-2dip"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/dishName"
                android:layout_width="fill_parent"
                android:layout_height="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:background="@drawable/borderforloginedittext"
                android:ems="10"
                android:hint="(require)"
                android:inputType="textCapWords|textPersonName"
                android:paddingLeft="70dp"
                android:textColor="#333333"
                android:textColorHint="#999999"
                android:textSize="16sp" >
            </EditText>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/secondRelativeLayout"
            android:layout_width="60dip"
            android:layout_height="44dip"
            android:layout_below="@+id/firstRelativeLayout"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="-2dip" >

            <TextView
                android:id="@+id/labelDish"
                android:layout_width="60dip"
                android:layout_height="44dip"
                android:gravity="center"
                android:text="DISH"
                android:textColorHint="#999999"
                android:textSize="10sp"
                android:visibility="visible" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/linearLayout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/secondRelativeLayout"
            android:layout_marginTop="-2dip"
             >

            <AutoCompleteTextView
                android:id="@+id/txtTags"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:minHeight="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:background="@drawable/borderforloginedittext"
                android:hint="eg: Breakfast, spicy, italian "
                android:inputType="textNoSuggestions|textMultiLine"
                android:imeOptions="actionNext"
                android:paddingLeft="70dip"     
                android:paddingTop="2dip"  
                android:paddingBottom="2dip"            
                android:dropDownHeight="fill_parent"
                android:dropDownWidth="match_parent"
                android:textColor="#333333"
                android:singleLine="false"
                android:textColorHint="#999999"
                android:textSize="16sp" >
            </AutoCompleteTextView>

            <RelativeLayout
                android:id="@+id/progressLayout"
                android:layout_width="23dip"
                android:layout_height="23dip"
                android:layout_alignRight="@+id/txtTags"
                android:layout_centerVertical="true"
                android:layout_marginRight="4dip"
                android:layout_marginTop="10dip"
                android:layout_below="@+id/linearLayout2"
                android:visibility="gone" >

                <ProgressBar
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </RelativeLayout>
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/thirdRelativeLayout"
            android:layout_width="60dip"
            android:layout_height="44dip"
            android:layout_below="@+id/secondRelativeLayout"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="-2dip" >

            <TextView
                android:id="@+id/labeltags"
                android:layout_width="60dip"
                android:layout_height="44dip"
                android:gravity="center"
                android:text="TAGS"
                android:textColorHint="#999999"
                android:textSize="10sp"
                android:visibility="visible" >
            </TextView>
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayout3"
            android:layout_marginTop="-2dip"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/menuPrice"
                android:layout_width="fill_parent"
                android:layout_height="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:background="@drawable/borderforloginedittext"
                android:ems="10"
                android:hint="(optional) "
                android:inputType="number"
                android:paddingLeft="70dp"
                android:textColor="#333333"
                android:textColorHint="#999999"
                android:textSize="16sp" >
            </EditText>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/fourthRelativeLayout"
            android:layout_width="60dip"
            android:layout_height="44dip"
            android:layout_below="@+id/linearLayout3"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="-2dip" >

            <TextView
                android:id="@+id/labelprice"
                android:layout_width="60dip"
                android:layout_height="44dip"
                android:gravity="center"
                android:text="PRICE"
                android:textColorHint="#999999"
                android:textSize="10sp"
                android:visibility="visible" >
            </TextView>
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fourthRelativeLayout"
            android:layout_marginTop="5dip" >

            <Button
                android:id="@+id/btnAddSecondaryHotel"
                android:layout_width="match_parent"
                android:layout_height="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:layout_marginTop="12dip"
                android:layout_weight="0.5"
                android:background="@color/disabledButtonColor"
                android:text="Let&apos;s go rate this dish!"
                android:textColor="@color/white"
                android:textSize="18sp" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="57dip"
    android:layout_height="57dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="30dip"
    android:layout_marginTop="100dip"
    android:src="@drawable/dishoomit" />

我很努力,但一无所获。请给我任何引用或提示。

最佳答案

您的代码仅在您执行 performScrollingUp() 之前检查 heightDiff 是否超过 100,并且您的评论表明这可能会在键盘弹出时发生。好吧,当您单击任何 editText 字段时,键盘会弹出...

修复:

尝试将您的第一个代码块更改为:

final View activityRootView = findViewById(R.id.RelativeAdd);
EditText tagEditText = findViewById(R.id.txtTags);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = activityRootView.getRootView()
                            .getHeight() - activityRootView.getHeight();
                    Log.i("TEST",
                            "GLOBAL LAYOUT " + activityRootView.getHeight());
                    Log.i("TEST", "GLOBAL LAYOUT rel"+ relativeLayoutHeight);
                    if (heightDiff > 100 && tagEditText.isFocused()) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        performScrollingUp();
                        Log.i("TEsT", "Keyboard visible");
} else {
    Log.i("TEsT", "Keyboard not visible");
    performScrollingDown();
    txtDishTags.setDropDownHeight(ViewGroup.LayoutParams.FILL_PARENT);
}
}
}); 

这将确保它仅在 tagEditText 被单击而不是其他时向上滚动。现在你只是在听听者的整体布局变化。让我知道这是否有效。

关于android - 单击任何 EditText 时屏幕向上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20885086/

有关android - 单击任何 EditText 时屏幕向上滚动的更多相关文章

  1. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  2. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  3. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  4. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

  5. 屏幕录制为什么没声音?检查这2项,轻松解决 - 2

    相信很多人在录制视频的时候都会遇到各种各样的问题,比如录制的视频没有声音。屏幕录制为什么没声音?今天小编就和大家分享一下如何录制音画同步视频的具体操作方法。如果你有录制的视频没有声音,你可以试试这个方法。 一、检查是否打开电脑系统声音相信很多小伙伴在录制视频后会发现录制的视频没有声音,屏幕录制为什么没声音?如果当时没有打开音频录制,则录制好的视频是没有声音的。因此,建议在录制前进行检查。屏幕上没有声音,很可能是因为你的电脑系统的声音被禁止了。您只需打开电脑系统的声音,即可录制音频和图画同步视频。操作方法:步骤1:点击电脑屏幕右下侧的“小喇叭”图案,在上方的选项中,选择“声音”。 步骤2:在“声

  6. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  7. ruby-on-rails - 如果条件与 &&,是否有任何性能提升 - 2

    如果用户是所有者,我有一个条件来检查说删除和文章。delete_articleifuser.owner?另一种方式是user.owner?&&delete_article选择它有什么好处还是它只是一种写作风格 最佳答案 性能不太可能成为该声明的问题。第一个要好得多-它更容易阅读。您future的自己和其他将开始编写代码的人会为此感谢您。 关于ruby-on-rails-如果条件与&&,是否有任何性能提升,我们在StackOverflow上找到一个类似的问题:

  8. ruby-on-rails - RoR中是否有任何内置方法可以为整数填充零? - 2

    如果我想要“00001”而不是“1”,除了我自己写填零方法之外,有没有内置的方法可以帮助我为整数填零? 最佳答案 puts"%05d"%1#00001参见:String::%,Kernel::sprintf这是正在发生的事情。%左侧的"%05d"是C风格的格式说明符。%右边的变量就是要格式化的东西。格式说明符可以像这样解码:%-格式说明符的开头0-用前导零填充5-长度为5个字符d-被格式化的是一个整数如果你要格式化多个东西,你会把它们放在一个数组中:"%d-%s"%[1,"One"]#=>1-one

  9. ruby-on-rails - 如何从按钮或链接单击的 View 调用 Rails 方法 - 2

    基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub

  10. ruby - 在 Mechanize 中使用 JavaScript 单击链接 - 2

    我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan

随机推荐