2014-09-05 61 views
0

Im建立一個應用程序,需要當你按下按鈕I.E. (搜索),然後打開搜索頁面,單擊(主頁)按鈕,然後返回到主頁視圖。我所缺少的是在這兩種觀點之間建立聯繫的知識。這就是主頁在XML中的樣子。「接線」按鈕更改在android中的視圖

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
//Title 


//Search Student Button 

<Button 
    android:id="@+id/button1" 
    android:layout_width="86dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:longClickable="false" 
    android:text="Search Student" /> 

//New Student Button 

<Button 
    android:id="@+id/button2" 
    android:layout_width="99dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:longClickable="false" 
    android:text="New Studetn " /> 

//法人按鈕

<Button 
    android:id="@+id/button3" 
    android:layout_width="82dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:longClickable="false" 
    android:text="Legal Info" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="40dp" 
    android:layout_above="@+id/button1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="66dp" 
    android:text="Student Registration " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

應用程序的一些照片。 http://imgur.com/aVpqUCZ

+0

'android:onClick = someFunction()'? – csmckelvey 2014-09-05 22:57:21

回答

0
Button searchStudentButton; 
Button newStudentButton; 
Button legalButton; 

searchStudentButton = (Button) findViewById(R.id.button1); 

searchStudentButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
    Intent myIntent = new Intent(CurrentActivity.this, searchStudentActivity.class); 
    CurrentActivity.this.startActivity(myIntent); 
    } 
}); 

newStudentButton = (Button) findViewById(R.id.button2); 

newStudentButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
    Intent myIntent = new Intent(CurrentActivity.this, newStudentActivity.class); 
    CurrentActivity.this.startActivity(myIntent); 
    } 
}); 

legalButton = (Button) findViewById(R.id.button3); 

legalButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
    Intent myIntent = new Intent(CurrentActivity.this, legalActivity.class); 
    CurrentActivity.this.startActivity(myIntent); 
    } 
}); 

不要忘記添加在AndroidManifest.xml新的活動:

<activity android:label="@string/app_name" android:name="searchStudentActivity"/> 
<activity android:label="@string/app_name" android:name="newStudentActivity"/> 
<activity android:label="@string/app_name" android:name="legalActivity"/> 
+0

是否有關係,我把活動代碼放在androidmanifest.xml中,或者我可以複製並粘貼到那裏 – 2014-09-08 01:03:11

+0

你可以複製粘貼在那裏,但'android:name =「searchStudentActivity」' - >'searchStudentActivity' should be你的項目中有一門課。這意味着''android:name = ClassName「' – 2014-09-08 06:04:50

0

當多個視圖和/或按鈕處理像你這樣,我通常喜歡使用只有一個所有視圖的實例onClickListener,以保持代碼更清潔。

public class MainActivity extends Activity implements View.OnClickListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.my_layout); 

     Button btnSearchStudent = (Button) findViewById(R.id.button1); 
     Button btnNewStudent = (Button) findViewById(R.id.button2); 
     Button btnLegalInfo = (Button) findViewById(R.id.button3); 

     btnSearchStudent.setOnClickListener(this); 
     btnNewStudent.setOnClickListener(this); 
     btnLegalInfo.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.button1: { 
       Intent intent = new Intent(this, SearchStudentActivity.class); 
       startActivity(intent); 
       break; 
      } 
      case R.id.button2: { 
       Intent intent = new Intent(this, NewStudentActivity.class); 
       startActivity(intent); 
       break; 
      } 
      case R.id.button3: { 
       Intent intent = new Intent(this, LegalInfoActivity.class); 
       startActivity(intent); 
       break; 
      } 
     } 
    } 
} 

我會建議你改變你的按鈕的android:id屬性更有意義的名稱。這樣可以更輕鬆地查看代碼中引用的內容。我個人比較喜歡在視圖類別的基礎上加上我的觀點,例如btn_對於Buttontv_對於TextView。請記住將您的電話更新爲findViewById(),並在switch聲明中使用該ID。

最後,不要忘記將您的活動添加到AndroidManifest.xml文件中,如Sagar Pilkhwal發佈,如果您還沒有。

相關問題