4

我有一个按钮,可以将您带到带有简短描述的示例图片,但我想做的是长按,然后让它将用户带到网站以获取更多信息。

这是我的按钮代码(正常)

    <Button
                    android:id="@+id/samplea"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/samplea_button" 
                    android:longClickable="true"/>

我的java是这个

Button next = (Button) findViewById(R.id.samplea);
next.setOnClickListener(new View.OnClickListener() {


        public void onClick(View view) {
            final ImageView imageView = (ImageView) findViewById(R.id.iM2);
            imageView.setImageResource(R.drawable.samplea_draw);

如何将 longclickable 添加到此以将我带到网站?有人可以帮忙吗?

我已经添加了它,但现在它似乎把我带到了这个网站(在长按之后),而不是图像(在正常的点击之后)这是我的代码:

  next1.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            // Launch your web intent
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/a/13298207/1267661"));
            startActivity(intent);
            return true;
        }

        public void onClick(View view) {
            final ImageView imageView = (ImageView) findViewById(R.id.iM2);
            imageView.setImageResource(R.drawable.samplea_draw);

在“public void onClick(View view) {”下得到一条黄线

4

3 回答 3

3

更新
实现一个 OnLongClickListener 很像您的 OnClickListener,但它需要分开。尝试这个:

Button next = (Button) findViewById(R.id.samplea);
next.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        // You can turn this into a class variable
        final ImageView imageView = (ImageView) findViewById(R.id.iM2);
        imageView.setImageResource(R.drawable.samplea_draw);
    }
)};
next.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // Launch your web intent
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/a/13298207/1267661"));
        startActivity(intent);
        return true;
    }
});
于 2012-11-08T21:34:27.677 回答
1

您添加了一个长按监听器 -

next.setOnLongClickListener(new OnLongClickListener() { 
    @Override
    public boolean onLongClick(View v) {
        //your action on long click
        return true;
    }
});

看这里 - Android:长按一个按钮 -> 执行操作

如果你先用谷歌搜索你的问题,你总是会更加努力地得到更好的答案!

于 2012-11-08T21:35:34.683 回答
0

按照下面的链接更详细

长按钮点击

请参阅下面的示例代码。

next.setOnLongClickListener(new OnLongClickListener() {

   @Override
   public boolean onLongClick(View v) {
    // TODO Auto-generated method stub

    Toast.makeText(MainActivity.this,"Button long click", Toast.LENGTH_SHORT).show();
    return true;
   }
  });
于 2012-11-09T08:38:36.230 回答