2013-07-03 92 views
1

我的項目只是爲了培訓目的而非常簡單。我試圖加載另一個界面/視圖/活動,當我點擊我的主佈局上的按鈕。以下是主要活動的代碼:只需點擊按鈕即可啓動另一項活動

package com.example.interfacemanipulation;

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends Activity { 

    public static final String EXTRA_MESSAGE = "Something Here...."; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void goToWelcomeActivityMethod(View view) 
    { 
     Intent intent = new Intent(this, WelcomeActivity.class); 
     EditText editText = (EditText) findViewById(R.id.welcomeMessage); 
     String message = editText.getText().toString(); 
     intent.putExtra(EXTRA_MESSAGE, message); 
     startActivity(intent); 
    } 

} 

而且這是在其他活動代碼:

package com.example.interfacemanipulation; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.TextView; 

public class WelcomeActivity extends Activity { 

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

     Intent intent = getIntent(); 
     String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
     TextView textView = new TextView(this); 
     textView.setTextSize(40); 
     textView.setText(message); 

     // Set the text view as the activity layout 
     setContentView(textView); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.welcome, menu); 
     return true; 
    } 

} 

這裏的代碼基本上是從谷歌文檔進行修改。我只是想能夠加載標籤中的第二個活動的視圖。這裏是我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.interfacemanipulation" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.interfacemanipulation.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.interfacemanipulation.WelcomeActivity" 
      android:label="@string/title_activity_welcome" > 
     </activity> 
    </application> 

</manifest> 

我只是一直在努力爲持續4天的android ...所以我感謝您的支持

+1

我不明白'goToWelcomeActivityMethod' ...或者是你遇到麻煩怎麼辦? –

+0

'setContentView(R.layout.activity_main);'您爲MainActivity的這個佈局是否爲WelcomeActivity定義了任何佈局? –

回答

2

你有2個問題:1。你沒有按鈕,帶你到活動(或者至少你還沒有在代碼中顯示它你發佈了,如果你這樣做,那麼你沒有爲該按鈕設置onClickListener)。 2.你不打電話給你的第二個活動的方法。

爲了有一個按鈕,把你帶到另一個活動,你需要將按鈕添加到佈局,然後添加onClickListener,它執行你的goToWelcomeActivityMethod,當你點擊它,就像這樣:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends Activity { 

    public static final String EXTRA_MESSAGE = "Something Here...."; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //this will insert the button into the Main Activity layout 
     Button goToWelcome = (Button)findViewById(R.id.goToWelcome); 

     //this is the onClickListener which will call the method to go to the next activity 
     goToWelcome.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
     { 
      goToWelcomeActivityMethod(); 
     } 
     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    //you don't need to pass in the View 
    public void goToWelcomeActivityMethod() 
    { 
     Intent intent = new Intent(this, WelcomeActivity.class); 
     EditText editText = (EditText) findViewById(R.id.welcomeMessage); 
     String message = editText.getText().toString(); 
     intent.putExtra(EXTRA_MESSAGE, message); 
     startActivity(intent); 
    } 

} 

然後在XML文件中,確保你添加太多的按鈕:在您調用

<Button 
     android:id="@+id/goToWelcome" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Go To Welcome" /> 
相關問題