首页 > 解决方案 > 我可以有一个完整的线性布局的点击事件吗

问题描述

我有一个线性布局和布局内的一些按钮。当单击线性布局时,它应该导航到新的 page1,当单击按钮时,它应该导航到 page2。

标签: xamarinxamarin.android

解决方案


你想达到这样的GIF效果吗? 在此处输入图像描述

如果是这样,是的,您可以为您的LinearLayout.There 添加点击事件。这是我的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"  android:id="@+id/my_ll">

<Button 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:text="click"
  android:id="@+id/button1"
 />

</LinearLayout>

这是背景代码。为线性布局和按钮添加不同的点击监听器。

    public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        LinearLayout my_ll = FindViewById<LinearLayout>(Resource.Id.my_ll);
        Button button1 = FindViewById<Button>(Resource.Id.button1); 
        my_ll.Click += My_ll_Click;
        button1.Click += Button1_Click;
    }

    private void Button1_Click(object sender, System.EventArgs e)
    {

        Intent intent = new Intent(this, typeof(Activity2));
        StartActivity(intent);
    }

    private void My_ll_Click(object sender, System.EventArgs e)
    {
        Intent intent = new Intent(this,typeof(Activity1));
        StartActivity(intent);
    }
}

推荐阅读