【问题标题】:How can i implement the longclickable in my app如何在我的应用程序中实现 longclickable
【发布时间】:2012-11-09 16:38:36
【问题描述】:

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

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

    <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){”下得到一条黄线

【问题讨论】:

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

标签: android android-layout android-intent


【解决方案1】:

更新
实现一个 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;
    }
});

【讨论】:

    【解决方案2】:

    你添加一个长按监听器 -

    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 搜索您的问题,您总是会更加努力地得到更好的答案!

    【讨论】:

      【解决方案3】:

      点击以下链接了解更多详情

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

      【讨论】:

        猜你喜欢
        • 2012-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 2010-09-07
        • 1970-01-01
        • 2023-04-07
        • 2021-11-17
        相关资源
        最近更新 更多