2014-01-27 46 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" 
相關問題