2016-05-04 8 views
1

を押したことを意味し、私はこれが私の最初のクラスである必要がありは、インテントを使用して新しいアクティビティを読み込みませんか?一度ボタンを

public void btnInput (View view) { 
    Intent intent = new Intent(this, inputTimetableEntry.class); 
    startActivity(intent); 
    } 

inputTimetableEntryは、私がリンク先のクラスです。

は、そのクラスの中で私は、これが第二のクラスに行くことを意味している

Intent intent = getIntent(); 

はかつて、ID btnInputとボタン上の任意の提案をクリックしましたか?

これが主な活動で使用されるXMLです:

<?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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.w15037204.w15037204assignment.timetableActivity"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="STUDENT TIMETABLE" 
     android:id="@+id/txtTitle" 
     android:textColor="#ff6a00" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textStyle="bold"/> 

    <ListView 
     android:layout_width="400dp" 
     android:layout_height="450dp" 
     android:id="@+id/listEntries" 
     android:layout_below="@+id/txtTitle" 
     android:layout_marginTop="10dp" /> 

    <Button 
     android:layout_width="180dp" 
     android:layout_height="70dp" 
     android:text="Input Entry" 
     android:id="@+id/btnInput" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:textColor="#ff6a00" 
     android:textSize="17dp" 
     android:textStyle="bold" 
     android:clickable="true" /> 
</RelativeLayout> 

+0

したがって、 'inputTimetableEntry'は' Activity'ですか? – Darwind

+0

アクティビティレイアウトxmlを投稿してください。 –

+0

はい@Darwindそれは、 – user6292035

答えて

0

あなたは正しくXMLを通じてビューにリスナーをアタッチ理解していませんでした。 xmlのビューにリスナーを付ける場合は、ビューのIDは何でも構いませんが、ビューにはonclick属性が設定されている必要があります。 だからあなたのビューは次のようになります -

<Button 
     android:layout_width="180dp" 
     android:layout_height="70dp" 
     android:text="Input Entry" 
     android:id="@+id/btnInput" //Any id here 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:textColor="#ff6a00" 
     android:textSize="17dp" 
     android:textStyle="bold" 
     android:onClick="sayHello" //Name which you would be using in your Activity 
     android:clickable="true" /> 

そして

public void sayHello(View view){ 
     //Code here 
    } 

また、あなたが動的にsetOnClickListener()あなたの活動にせずに設定できるパラメータとしてView参照を受け入れ同じ名前sayHelloであなたの活動にメソッドを定義しますonClick属性がXMLにあります。

Button btnInput = (Button)findViewById(R.id.btnInput); 
    btnInput.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //Code here 
       } 
      }); 
+0

ありがとう!私は今この問題を解決しましたが、これは他人のための良い答えを示しています! – user6292035

関連する問題