0

我在 2 个地方遇到错误,我不知道为什么,因为我是 Android 新手...

public class MainActivity extends Activity {

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

@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 onTouchEvent(MotionEvent event)
 {
    Context context=getApplication();
    CharSequence text = "This is for ontouch event";
    int duration = Toast.LENGTH_SHORT;
     Toast Msg = Toast.makeText(context,text,duration);
     int x=(int)event.getX();
     int y=(int)event.getY();
     Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
     Msg.show();

     return true;
 } //Error type Syntax error on token "}", delete this token    

TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();

t1.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
});
}  // Error Syntax error, insert "}" to complete ClassBody
4

7 回答 7

2

在方法中编写您的 TextView 代码onCreate(),您不能按照您编写的方式在任何函数之外编写此代码。

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

    TextView t1=(TextView)findViewById(R.id.text);
    t1.isClickable();

    t1.setOnLongClickListener(new View.OnLongClickListener() {
       @Override
       public boolean onLongClick(View v) {
          // TODO Auto-generated method stub
          Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
          return true;
       }
   });
}
于 2013-06-27T05:14:53.680 回答
1

粘贴整个代码并试试这个

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView t1=(TextView)findViewById(R.id.text);
   t1.isClickable();
   t1.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(getBaseContext(), "Text View Clicked",Toast.LENGTH_LONG).show();
        return false;
    }
    });
}

@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 onTouchEvent(MotionEvent event)
 {
    Context context=getApplication();
    CharSequence text = "This is for ontouch event";
    int duration = Toast.LENGTH_SHORT;
     Toast Msg = Toast.makeText(context,text,duration);
     int x=(int)event.getX();
     int y=(int)event.getY();
     Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
     Msg.show();

     return true;
  //Error type Syntax error on token "}", delete this token    


}  // 
}

这肯定可以工作并长时间单击文本。肯定会为您提供输出。

于 2013-06-27T06:20:49.293 回答
1

把这个移到里面onCreate

  TextView t1=(TextView)findViewById(R.id.text);
  t1.setOnLongClickListener(new View.OnLongClickListener() {
  @Override
  public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this ,"Long Clicked", Toast.LENGTH_SHORT).show();
    return true;
}
});
于 2013-06-27T05:02:04.610 回答
0

尝试这个,

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.textView1);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
    Toast.makeText(getBaseContext(), "Text View Clicked",Toast.LENGTH_LONG).show();
    return false;
}
});

}

长时间点击文字,不要快速离开。

它工作正常。

于 2013-06-27T05:30:28.897 回答
0

此代码在任何函数之外:

TextView t1=(TextView)findViewById(R.id.text);
    t1.isClickable();

    t1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
            return true;
        }
    });

检查您的功能结构。

于 2013-06-27T05:01:47.860 回答
0

代码的最后两段需要一个函数来包装它们,可能是onCreate. 您需要调用setLongClickable视图进行长按注册。

于 2013-06-27T05:02:02.623 回答
0

试试这个代码...

类文件...

package com.longclick;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    TextView t1=(TextView)findViewById(R.id.text);
       t1.isClickable();

      t1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
           // TODO Auto-generated method stub
           Toast.makeText(getApplicationContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
           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.activity_main, menu);
    //return true;
//}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    Context context=getApplication();
    CharSequence text = "This is for ontouch event";
    int duration = Toast.LENGTH_SHORT;
     Toast Msg = Toast.makeText(context,text,duration);
     int x=(int)event.getX();
     int y=(int)event.getY();
     Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
     Msg.show();

    return true;
} //Error type Syntax error on token "}", delete this token    


}

.xml 文件

<RelativeLayout 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=".MainActivity" >

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

</RelativeLayout>

我认为它对你有用......

在此处输入图像描述

演示在这里

于 2013-06-27T05:19:36.857 回答