滚动时帮我改变文字 [英] help me change the text when I scroll

查看:58
本文介绍了滚动时帮我改变文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Android Studio创建一个应用程序,在滚动时会显示一条消息,在本例中为manusText。我显然做错了,因为事实并非如此。这是我的代码:



首先我导入所有包裹



package com.sceptech.scrolly;



导入android.support.v7.app.AppCompatActivity;

导入android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.MotionEvent;

import android.widget。 RelativeLayout;

导入android.view.View;

导入android.widget.TextView;

导入android.widget.Button;

导入android.graphics.Color;

导入android.view.GestureDetector;

导入android.support.v4.view.GestureDetectorCompat;



然后我通过实现监听器来跟进,以便代码注册所需的交互,在本例中是滚动。



公共类scrolly_menu扩展AppCompatActivity实现了GestureDetector.OnGestureListener {



私有TextView manusText;

私人GestureDetectorCompat gestureDetector;





@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);



//设置背景



RelativeLayout manusLayout = new RelativeLayout(this);

manusLayout.setBackgroundColor(Color.parseColor(#2BABD6));



//创建标题和按钮



final TextView manusText = new TextView(this);

manusText.setText(Hello!) ;



按钮manusButton = new Button(this);

manusButton.setText(Click Me);

manusButton.setBackgroundColor(Color.parse颜色(#2056E8));



manusButton.setId(1);

manusText.setId(2);





//设置标题和按钮的布局参数



RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT

);



RelativeLayout.LayoutParams textDetails = new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT

);



buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);

buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);



textDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

t extDetails.addRule(RelativeLayout.ABOVE,manusButton.getId());

textDetails.setMargins(0,0,0,50);





manusLayout.addView(manusButton,buttonDetails);

manusLayout.addView(manusText,textDetails);





setContentView(manusLayout);



创建一个点击监听器,当用户点击按钮时,显示的文字会发生变化。(这是有效的)



manusButton.setOnClickListener(

new Button.OnClickListener(){

public void onClick(查看V){

manusText.setText(Good Job Manu!);

}

}

);



this.gestureDetector = new GestureDetectorCompat(this,this);



}



最后实现代码。



@Override

公共布尔值onDown(MotionEvent e){

返回true;

}



@Override

public void onShowPress(MotionEvent e){



}



@Override

public boolean onSingleTapUp(MotionEvent e){



返回true;

}



@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX,float distanceY){

manusText.setText(Scroller);

返回true;

}



@Override

public void onLongPress(MotionEvent e){



}



@Override

public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){

return是的;

}



@Override

public boolean onTouchEvent(MotionEvent event){

this.gestureDetector.onTouchEvent(event);

返回super.onTouchEvent(事件);

}

}

I'm trying to make an app with Android Studio that displays a message,in this case manusText, when scrolled. I'm clearly do something wrong as this is not the case. Here is my code:

First I import all the packages

package com.sceptech.scrolly;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.graphics.Color;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;

Then I follow up by implementing the listener so that the code registers the desired interaction, in this case the scroll.

public class scrolly_menu extends AppCompatActivity implements GestureDetector.OnGestureListener {

private TextView manusText;
private GestureDetectorCompat gestureDetector;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set the background

RelativeLayout manusLayout = new RelativeLayout(this);
manusLayout.setBackgroundColor(Color.parseColor("#2BABD6"));

// Create a title and button

final TextView manusText = new TextView(this);
manusText.setText("Hello!");

Button manusButton = new Button(this);
manusButton.setText("Click Me");
manusButton.setBackgroundColor(Color.parseColor("#2056E8"));

manusButton.setId(1);
manusText.setId(2);


// Set layout parameters for title and button

RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);

RelativeLayout.LayoutParams textDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);

buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

textDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
textDetails.addRule(RelativeLayout.ABOVE, manusButton.getId());
textDetails.setMargins(0, 0, 0, 50);


manusLayout.addView(manusButton, buttonDetails);
manusLayout.addView(manusText, textDetails);


setContentView(manusLayout);

Create a click listener such that when the user click on the button the text displayed changes.(This works)

manusButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View V) {
manusText.setText("Good Job Manu!");
}
}
);

this.gestureDetector = new GestureDetectorCompat(this, this);

}

Finally implement the code.

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {

return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
manusText.setText("Scroller");
return true;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}

推荐答案

尝试实现.OnScrollListener



或让它响应触摸,这样当你开始滚动文本Scoller出现时



Try implementing a .OnScrollListener

or make it respond to touch so that when you begin scrolling the text "Scoller" appears

myLayout.setOnTouchListener(
       new RelativeLayout.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent m) {
            // Perform tasks here
            return true;
        }
       }
);


这篇关于滚动时帮我改变文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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