我试图根据this link,运行“清洁代码”但是没有发生任何事情。作为一名Renegade开发人员,我的直觉说“Button onClick”不见了,但我可能错了。这是根据说明书编写的代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class BuildingaSimpleUserInterfaceApi8Activity extends Activity {
public final static String EXTRA_MESSAGE = "com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**This is how I work with Buttons and the Next Activity, however this method IS NOT defind
* in the very first Android Tutorial for sending information onClick.
Button bButtonone = (Button) findViewById(R.id.button_send);
bButtonone.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY"));
}
});
}
*/
/** Called when the user selects the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developer.android.com_training_basics_firstapp_building_ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BuildingaSimpleUserInterfaceApi8Activity"
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.developer.android.com_training_basics_firstapp_building_ui.DisplayMessageActivity" />
</application>
</manifest>
答案 0 :(得分:2)
您似乎忘记了开始活动的行:
public void sendMessage(View view) {
...
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent); // add me!
}
答案 1 :(得分:1)
我仔细阅读了所列网站上的代码,但您错过了一行:
startActivity(intent);
最后是sendMessage。