按Alt / Enter键导入缺失的类

时间:2016-12-04 16:43:52

标签: android android-studio android-intent android-activity

我在这里运行教程:https://developer.android.com/training/basics/firstapp/starting-activity.html并在标题为"开始另一项活动"的页面上,标题为"构建意图"

按照说明操作后

  

在MainActivity.java中,将下面显示的代码添加到sendMessage()

MainActivity.java 如下所示:

package com.example.myfirstapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class MainActivity extends AppCompatActivity {

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

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        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);
        startActivity(intent);
    }
}

然后说明说要按 Alt / Enter 导入缺少的类。按 Alt / Enter 后,代码看起来像这样(唯一的变化是在导入中):

    package com.example.myfirstapp;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import static android.provider.AlarmClock.EXTRA_MESSAGE;

    public class MainActivity extends AppCompatActivity {

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

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        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);
        startActivity(intent);
    }
}

    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        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);
        startActivity(intent);
    }
}

然后工作室再次按 Alt / Enter (我想解决EXTRA_MESSAGE)。所以我做了。这会创建一个对话框来创建常量字段' EXTRA_MESSAGE'"。但我不知道给它带来什么价值。

第一次按 Alt / Enter 不会产生预期的结果。该教程说导入应该如下所示:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

现在我对下一步该做什么感到困惑。

有人可以对教程中可能缺少的内容发表评论吗?

0 个答案:

没有答案