我試圖創建一個簡單的程序,它從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>
我改變了它onTouch到的onClick現在,無論我觸摸屏幕的位置如何,隨機文本視圖都會消失。 編輯:它不是隨機的。當點擊右下角時,它將從9倒數到0的可見性設置爲0。如何將其更改爲邊距不激活點擊的位置? – 2014-11-06 05:49:15
@PythonNoob請用您的新代碼更新您的問題 – 2014-11-06 05:51:20
@PythonNoob請注意,您應該在Textview中設置onClickListener – 2014-11-06 05:51:53