我正在製作一個應用程序,其中需要監聽片段小部件上的單擊事件。 在我的應用程序中: 我將我的活動的默認片段設置爲fragmentOne,並且我想在有人單擊fragmentOne中的某個小部件時更改活動的片段,我想將片段的活動從fragmentOne更改爲fragmentTwo。 這裏是我的主要活動的XML代碼:如何在android中的活動中偵聽片段中的點擊?
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.wordpress.tbkj.app_name.LoginSignupActivity">
<fragment
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/login_signup_fragment"
android:name="com.wordpress.tbkj.app_name.LoginFragment"
tools:layout="@layout/fragment_login" />
</RelativeLayout>
這裏是我的活動代碼:
public class LoginSignupActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_signup);
}
}
當我啓動這一活動fragmenOne是可見的。 如果有人想要其他代碼,請發表評論。
你能分享一些代碼嗎?在片段中,您還可以在視圖上使用setOnClickListener方法。 – AppPhil