2014-11-06 140 views
1

我试图创建一个简单的程序,它从0-9随机生成数字到屏幕上,然后当您触摸它时,它们就会消失。我遇到的问题是当我触摸屏幕上的空白时,数字消失。我如何防止这种情况发生?这是因为利润率?触摸侦听器侦听边距

MainActivity.java(UPDATE:改变了的onClick为内部填入())

package com.example.textdisappear; 

import java.util.Random; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Display; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.FrameLayout; 
import android.widget.TextView; 

public class MainActivity extends ActionBarActivity 
{ 
    private FrameLayout layout; 
    private Random rand; 
    private int left, top, height, width, textSizeBufferL, textSizeBufferT, counter; 
    private Display display; 
    private FrameLayout.LayoutParams rlp; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     display = getWindowManager().getDefaultDisplay(); 
     height = display.getHeight(); 
     width = display.getWidth(); 
     rand = new Random(); 
     layout = (FrameLayout)findViewById(R.id.layout); 
     textSizeBufferL = width/9; 
     textSizeBufferT = height/7; 
     counter = 0; 
     populateLayout(); 
    } 

    public void populateLayout() 
    { 
     layout.removeAllViews(); 
     for(int x = 0; x < 10; x++) 
     { 
      TextView view = new TextView(this); 
      view.setOnClickListener(new View.OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
        v.setVisibility(View.INVISIBLE); 
        counter++; 
        if(counter >= 10) 
        { 
         counter = 0; 
         populateLayout(); 
        } 
       } 
      }); 
      rlp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
      do 
      { 
      left = rand.nextInt(width); 
      //right = rand.nextInt(width); 
      } while(left + textSizeBufferL > width); 

      do 
      { 
      top = rand.nextInt(height); 
      //bottom = rand.nextInt(height); 
      } while(top + textSizeBufferT > height); 

      rlp.setMargins(left, top, 0, 0); 
      view.setLayoutParams(rlp); 
      view.setText(""+x); 
      view.setTextSize(15); 
      //view.setId(x+1); 
      layout.addView(view); 
     } 
     int x = 0; 
     if(x == 0); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) 
     { 
      return true; 
     } 
     if (id == R.id.reset) 
     { 
      counter = 0; 
      populateLayout(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

activity_main.xml中

<FrameLayout 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" 
    tools:context="com.example.textdisappear.MainActivity" > 

    <FrameLayout 
     android:id="@+id/layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

回答

1

我遇到的问题是,当我触摸白色空间在屏幕上 数字消失。

是的,因为您重写了在触摸整个屏幕中任何位置时将被调用的onTouch方法。

@Override 
public boolean onTouch(View v, MotionEvent event) 
{ 
    //move this whole code to onClick for textviews 
    v.setVisibility(View.INVISIBLE); 
    if(counter < 10) 
    { 
     counter++; 
     return false; 
    } 
    if(counter == 10) 
    { 
     counter = 0; 
     populateLayout(); 
     return false; 
    } 
    return false; 
} 

如果你想如果textview被触摸的textview才会消失,用onClick代替,并设置在textview。有了这个,代码仅在textview被点击时调用,不是整个屏幕。

更新1

你在错误的地方设置的onClick。你应该做这样的事情populateLayout

TextView view = new TextView(this); 
view .setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
       v.setVisibility(View.INVISIBLE); 
    counter++; 
    if(counter >= 10) 
    { 
     counter = 0; 
     populateLayout(); 
    } 
    } 

});

之后,删除您overrided的onClick和世界上没有必要impelemnt onClickListener的活动:

//delete this code 
@Override 
    public void onClick(View v) 
    { 
     v.setVisibility(View.INVISIBLE); 
     counter++; 
     if(counter >= 10) 
     { 
      counter = 0; 
      populateLayout(); 
     } 
    } 

更新2 并改变rlp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);WRAP_CONTENT

+0

我改变了它onTouch到的onClick现在,无论我触摸屏幕的位置如何,随机文本视图都会消失。 编辑:它不是随机的。当点击右下角时,它将从9倒数到0的可见性设置为0。如何将其更改为边距不激活点击的位置? – 2014-11-06 05:49:15

+0

@PythonNoob请用您的新代码更新您的问题 – 2014-11-06 05:51:20

+0

@PythonNoob请注意,您应该在Textview中设置onClickListener – 2014-11-06 05:51:53