2014-07-01 21 views

回答

1
myView = findViewById(r.id.my_view); 
    myView.setOnLongClickListener(new OnLongClickListener() { 
    public boolean onLongClick(View arg0) { 
    Toast.makeText(getApplicationContext(), "Long Clicked " ,Toast.LENGTH_SHORT).show(); 

     return true; // set to true 
    } 
}); 
+0

這對我的申請不公平。我需要在當前的活動中進行長時間的記憶。我需要長久的觀點。如果有任何其他方式呢? – user3759635

+0

只需通過yourViewOrLayout更改按鈕即可。 – MSR

0

你的類可以使用onLongClickListener接口。

您的類擴展了Activity實現了View.OnLongClickListener,它具有長按通知的方法。

不要忘記設置yourView.setOnLongClickListener(this);爲你所需要的所有觀點。

0

例如,如果你有一個頂級的佈局像一個LinearLayout中,設置該上LongClick:

你layout.xml

<LinearLayout 
android:id="@+id/your_layout" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

你Activity.java

LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_layout); 

yourLayout.setOnLongClickListener(new OnLongClickListener() { 

    @Override 
     public boolean onLongClick(View v) { 
     Toast.makeText(yourMainActivity.this, "Your Text", Toast.LENGTH_SHORT).show(); 
     return true; 
    } 

    }); 
0

若要在android中查看longpress任何地方的烤麪包 -

長按VIEW===>

enter image description here


LongPress.java類 -

public class LongPress extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_long_press); 
    TextView txtView = (TextView) findViewById(R.id.txtView); 
    txtView.setOnLongClickListener(new OnLongClickListener() { 
    @Override 
    public boolean onLongClick(View v) { 
    // TODO Auto-generated method stub 
    Toast.makeText(getApplicationContext(), 
     "You have pressed it long :)", 2000).show(); 
    return true; 
    } 
    }); 
    txtView.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Toast.makeText(getApplicationContext(), "Not Long Enough :(", 
     1000).show(); 
    } 
    }); 
} 

} 

相關佈局文件 -

<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" 
android:gravity="center" 
tools:context=".LongPress" > 

<TextView 
android:id="@+id/txtView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true" 
android:layout_alignParentRight="true" 
android:layout_alignParentTop="true" 
android:gravity="center" 
android:text="Long Press Me!!!!" /> 

</RelativeLayout> 

來源:android-long-press-event-handle-example