2014-01-27 131 views
2

我有一个布局(MenuLayout),其中我有一个textView(注销)。我想在textView中添加一个点击监听器。我将这个布局包含在另一个布局中。在包含的布局的文本视图中添加点击监听器

下面是代码

<p.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/FlyOutContainer"> 
<include 
    layout="@layout/menuLayout"/> 
<include 
    layout="@layout/Home" /> 

我写单击事件代码注销这样

TextView logout = FindViewById<TextView> (Resource.Id.textViewLogout); 


     logout.Click += delegate { 
      Intent logOut = new Intent(this,typeof(MainActivity)); 
      StartActivity(logOut); 
     }; 

但Click事件不工作。 任何人都可以告诉我解决方案我犯的错误。

这是我使用的源代码的Link

+1

这是机器人?如果是的话,你正在使用原生android sdk和eclipse? –

+1

是的,这是在android中,但在xamarin(语言c#)。 –

回答

1

尝试这样的:

findViewById(R.id.logout_button).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), YourActivity.class); 

       startActivity(intent); 

      } 
     }); 
+0

其不工作 –

0

在Andrioid使用Java:

对于<include>一是虚增您的布局像

View view = getLayoutInflater().from(this).inflate(R.layout.menuLayout, null) 

Button btn = (Button) view.findViewById(R.id.arrow_down); 

btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 

希望这可以给你一个想法转换成C#( Xamarin)

4

Xamarin的另一种方法是:

TextView logout = FindViewById<TextView> (Resource.Id.textViewLogout); 

logout.Click += logout_Click; 


void logout_Click(object sender, EventArgs e) 
{ 
    Intent logOut1 = new Intent(this,typeof(MainActivity)); 
    StartActivity(logOut1); 
} 

编辑:尝试使用其他名称为您的意图,以防万一。

+1

我这样做,但它不工作。米不能点击文字 –

+0

你可以做'logout.Click + ='(然后按键盘上的tab键两次)来生成事件,然后粘贴你的代码... – Milen

+0

仍然我无法点击任何textView。请检查此[链接](http://blog.neteril.org/blog/2013/04/19/fly-out-menu-xamarin-android/)。这个代码我正在使用。 –

0

添加到您的TextView:

android:clickable="true" 
相关问题