[英]i want to open an image in another activity when clicked on the item in the listview
[英]I want to start another activity when it is clicked?
好吧,我有一個活動,除了棕色背景的相對布局外,什么都不包含,如果用戶單擊屏幕上的任意位置,我想開始另一個活動,我該怎么做?
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
}
首先,通過將android:id="@+id/relativeLayout"
放入布局文件中,為您的RelativeLayout
提供ID。
RelativeLayout l = (RelativeLayout) findViewById(R.id.relativeLayout1);
l.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// startActivity here
}
});
或不使用您的RelativeLayout
只需實現活動的onTouchEvent(MotionEvent)
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
// start activity
}
return super.onTouchEvent(event);
}
注意:尚未嘗試此代碼。
將onClick屬性添加到布局xml,並在活動中實現onClick方法以啟動新活動。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:orientation="vertical" >
在您的活動中添加一個onClick方法。
public void OnClick(View v) {
startActivity(new Intent(this,MyNewActivity.class));
}
我認為您的主屏幕的父級布局為Linear,Relative,Frame。 以此作為參考並處理點擊監聽器。
例如:如果您的父級布局是線性的。
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
linear.setOnClickListener(this);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.