[英]android - make textView invisible
When my app starts, the user needs to touch on the screen before the real action starts. 当我的应用启动时,用户需要先触摸屏幕,然后才能执行实际操作。 I have a textView
which gives the hint to touch the screen. 我有一个textView
,它给出了触摸屏幕的提示。
After the screen is touched, I want the text to get invisible . 触摸屏幕后,我希望文本不可见 。 Right now the textView
never disappears and always stays in the front. 现在, textView
永远不会消失,始终停留在最前面。
public class MainActivity extends Activity implements OnGestureListener
{
public boolean touched = false;
TextView mMyView;
public void onTouch()
{
mMyView.setVisibility(View.GONE);
touched = true;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mMyView = (TextView)findViewById(R.id.textView6);
if(touched == true)
{
}
}
}
1.Always use if(something)
if you want to see if it's true/false
instead of writing if(something == true)
[something is a boolian assigned with value true.] 1. if(something)
要查看它是否为true/false
则始终使用if(something)
而不是编写if(something == true)
[something是一个布尔值,赋值为true。]
2.If you point your views xml to a method using android:onClick
like below, 2.如果您将xml指向使用android:onClick
的方法,如下所示,
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="onTouch" />
. 。 What's the point of implementing OnGestureListener
? 实现OnGestureListener
的意义OnGestureListener
?
If i do this onCreate
i initialize my view 如果我在onCreate
执行此操作, onCreate
初始化我的视图
View myView = findViewById(R.id.my_view);
3.If i really want a touch i will do this 3.如果我真的想要触摸我会这样做
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// ... Respond to touch events --> tv.setVisibility(View.INVISIBLE);
return true; // if you return false with this then the listener will not be called for the subsequent ACTION_MOVE and ACTION_UP string of events.
}
});
MotionEvent
, you can identify the motion ACTION_DOWN
, ACTION_MOVE
and ACTION_UP
现在,您可以在第3个参数中看到一个MotionEvent
,可以识别动作ACTION_DOWN
, ACTION_MOVE
和ACTION_UP
Now think have you ever used them. 现在想想您是否曾经使用过它们。 You got an idea in your head about a touch so tried to use touch events .. But you don't use them. 您对触摸有个主意,因此尝试使用触摸事件..但是您不使用它们。 So it does the same as what onClickListner
does in your case. 因此,它与您的案例中的onClickListner
一样。 If you want motions use that 3rd one i gave. 如果您想使用我给的第三个动作。
But simply you can use 但是您可以简单地使用
// view is the background layout
myView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Do something here --> Hide your text tv.setVisibility(View.INVISIBLE);
}
});
Those view onClickListner
or setOnTouchListener
you can directly use them inside onCreate
or keep them inside a method and you can call that method from onCreate
. 这些浏览onClickListner
或setOnTouchListener
你可以直接使用它们里面onCreate
或者让他们的方法里,你甚至可以从该方法onCreate
。 Why to keep a boolean? 为什么要保留布尔值? It's nothing major 没什么大不了的
Note i considered myView as the background layout not your textView , background is the one you click / touch 注意我认为myView是背景布局,而不是您的textView,background是您单击/触摸的背景
So now you changed the questions code several times and I hope it´s the final change. 因此,现在您多次更改了问题代码,我希望这是最终的更改。 Only than my answer could help. 仅比我的答案有用。
You have done this in your onCreate()
: 您已经在onCreate()
完成了此操作:
if(touched == true)
{
tv.setVisibility(View.INVISIBLE);
}
But this is executed directly and has nothing to do with you onTouch()
method. 但这是直接执行的,与您的onTouch()
方法无关。 Let´s assume your onTouch()
works correctly. 假设您的onTouch()
正常工作。 Make the TextView
global : 使TextView
全局 :
TextView mMyView;
initialize it in onCreate()
: 在onCreate()
初始化它:
mMyView = (TextView)findViewById(R.id.textView6);
and then hide it in onTouch()
: 然后将其隐藏在onTouch()
:
onTouch(View view){
mMyView.setVisibility(View.GONE);
}
But you have to be sure that your method onTouch()
works. 但是您必须确保您的方法onTouch()
有效。 You can make a Toast
or a Log to check. 您可以制作Toast
或日志进行检查。 You have to be sure that: 您必须确保:
-The TextView
is inside your layout xml that you set with setContentView(R.layout.activity_game);
TextView
位于您使用setContentView(R.layout.activity_game);
设置的布局xml中setContentView(R.layout.activity_game);
-The onTouch()
method is declared in your TextView
's xml attribute -在您的TextView
的xml属性中声明了onTouch()
方法
android:onClick="onTouch"
and set clickable of your TextView
to true: 并将TextView
clickable设置为true:
android:clickable="true";
If you implement onGestureListener()
I guess the touch event is consumed by the listener and your TextView
did not recognize onTouch()
. 如果您实现onGestureListener()
我认为触摸事件已由侦听器占用,并且您的TextView
无法识别onTouch()
。 If you don´t do any gesture detection in your activity, then remove this implementation. 如果您在活动中未进行任何手势检测,请删除此实现。
You are checking if screen was touched in onCreate()
which is called only once at the start of the activity. 您正在检查是否在onCreate()
触摸了屏幕,该onCreate()
在活动开始时仅被调用一次。 Initialize TextView globally and set its visibility inside onTouch(View v, MotionEvent event)
Also your onTouch()
isn;t correct. 全局初始化TextView并在onTouch(View v, MotionEvent event)
onTouch()
onTouch(View v, MotionEvent event)
内设置其可见性另外,您的onTouch()
是不正确的。 You should override public boolean onTouch(View v, MotionEvent event)
您应该重写public boolean onTouch(View v, MotionEvent event)
public class MainActivity extends Activity
{
public boolean touched = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
TextView tv = (TextView) findViewById(R.id.textView6);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
touched = true;
tv.setVisibility(View.INVISIBLE);
return true;
}
});
}
}
Instead of implementing OnGestureListener
add a setOnTouchListener
in your root view of your activity layout 除了实现OnGestureListener
setOnTouchListener
,还可以在活动布局的根视图中添加setOnTouchListener
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlTestView"/>
For example rlTestView
is your activity's root layout id, then use below code in your oncreate
method 例如rlTestView
是您活动的根布局ID,然后在oncreate
方法中使用以下代码
((RelativeLayout)findViewById(R.id.rlTestView)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
tv.setVisibility(View.GONE);
return true;
}
});`
Use the code below on the onCreate
method and yes set the visibility
as GONE
instead of invisible. 在onCreate
方法上使用以下代码,并且将visibility
设置为GONE
而不是不可见。 Also state the current visibilty of the TextView
in the onTouch
then set it to 还规定了当前公开程度 TextView
在onTouch
然后将其设置为
tv.setVisibility(View.GONE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.