2012-11-08 83 views
4

我有一个按钮,你需要一个简短的描述示例图片,但我想要做的就是长时间点击,然后让它把用户带到一个网站更多信息。我怎样才能实现可在我的应用程序中的longclickable

这里是我为我的按钮(正常)

<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添加到该带我去一个网站吗? 任何人都可以请帮忙吗?

我已经加入了,但现在看来要带我到这个网站(longclicking后),而不是像(正常的onclick后)我的继承人代码:

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); 

得到一条黄线在“公共无效的onClick(查看图){”

+0

你不能像这样组合一个OnClickListener和OnLongClickListener,我更新了我的答案。 – Sam

回答

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; 
    } 
}); 
+0

非常感谢你,它工作的很棒!谢谢!!!! – Allrounder

1

你长按监听器添加一个 -

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

看吧 - Android: long click on a button -> perform actions

你总是会得到更好的答案与你的第一个Google你的问题更多的努力!

0

按照下面链路更详细

Long Button click

参见下面的示例代码。

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; 
    } 
    }); 
相关问题