我的Android应用中有一个字符串生成器功能。我希望每次有“点击”或“触摸”时调用字符串生成器功能。然后,字符串生成器函数将更改文本标签中的字符串。我没有任何类型的错误,但是当我在Android模拟器和物理设备上测试应用程序时,文本标签中的字符串不会更改,这意味着不会调用字符串生成器函数。
以下是我的代码:
public class MainActivity extends Activity{
//member variables
private ExcuseGenerator mExcuseGenerator = new ExcuseGenerator();
private ImageView mMrExcuse;
private TextView mExcuse;
private RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMrExcuse = (ImageView) findViewById(R.id.imageView1);
mExcuse = (TextView) findViewById(R.id.textView1);
rl = (RelativeLayout) findViewById(R.id.rl1);
rl.setOnTouchListener(
new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
//here is the string generator function
excuseGenerator();
return true;
}
return false;
}
}
);
}
注意:我希望在屏幕上的任何位置检测点击/触摸,这就是我使用OnTouchListener的相对布局的原因。
编辑:添加了屏幕截图以显示XML文件。
编辑2:这是我的借口生成器功能:
private void excuseGenerator(){
String excuse = mExcuseGenerator.getExcuse();
mExcuse.setText(excuse); //mExcuse is a text label.
}
以下是mExcuseGenerator类的getExcuse函数:
/*
* I want to create a "real random experience".
* This function will have 2 lists. Every time an excuse is generated,
* it is deleted from its original array and then put into a temporary array
* so that the same excuses are not to be seen over and over again.
* when the original array is emptied, it will be reassigned the set of excuses
* and the temporary array will be emptied again.
*/
public String getExcuse(){
if(stringArray.isEmpty()){
stringArray = Arrays.asList(mExcuseList);
tempHolder = null;
}
String excuse;
Random randomGenerator = new Random();
// get the position of the element from the array
int randomElement = randomGenerator.nextInt(stringArray.size());
excuse = stringArray.get(randomElement);
tempHolder.add(stringArray.get(randomElement));
stringArray.remove(stringArray.get(randomElement));
//return the element
return excuse;
}
答案 0 :(得分:5)
如果要在屏幕上的任何位置启用touchListener,请使用此方法OnTouchEvent(MotionEvent event)
。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN : //Do something
break;
case MotionEvent.ACTION_UP : //Do something
break;
}
return true;
}
@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;
}
现在您不必检测特定视图的touchEvents。
答案 1 :(得分:1)
请在ACTION_DOWN(http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN)事件发生时尝试返回true。这是首次触发的事件,如果在这种情况下返回false,如果我没记错,则不会触发所有其他事件。
答案 2 :(得分:0)
添加
android:clickable="true"
到你的布局的XML中的RelativeLayout。
<强> activity_main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/clicks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
并在你的java代码中, 的 MainActivity.java 强>
package com.test.testing;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout click = (RelativeLayout) findViewById(R.id.clicks);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_SHORT).show();
}
});
}
@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;
}
}