构建第一个应用教程按钮不工作



所以,我目前正试图在android页面上制作一个android应用程序的教程,建立你的第一个应用程序(https://developer.android.com/training/basics/firstapp/index.html?hl=it)我是在Intellji上做的,所以我发现它有点不同。所以,我完成了第三部分,我做了界面,但在第四部分,我创建了另一个活动,事情变得奇怪了。

我创建了displaymessageactivity.java文件,并从站点复制了代码。然后我发现的问题是,由于文件不是自动生成的,或者出于其他原因,Intellij不知道如何处理R.id。Action_settings和容器。因此,我查看了代码,并认为现在,对于"开始另一个活动"部分,我只需要"onCreate"方法。所以我现在把onoptionsitemselected布尔值变成灰色。

当我运行这个,然而,界面出来了,但发送按钮没有做任何事情,当它应该显示一个新的页面与消息…所以,我想知道如何解决这个问题。

这些是我的代码

MyActivity.java

package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MyActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = String.valueOf(editText.getText());
    intent.putExtra(EXTRA_MESSAGE, message);
}
}

DisplayMessageActivity.java

    package com.example.myfirstapp;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.myfirstapp.R;
public class DisplayMessageActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
    TextView textview = new TextView(this);
    textview.setTextSize(40);
    textview.setText(message);
    setContentView(textview);
}
public static class PlaceholderFragment extends Fragment {
    public PlaceholderFragment() { }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_display_message, container, false);
        return rootView;
    }
}
}

如果你想有一个循序渐进的Android开发教程,我认为这个网站将是最适合你的:http://www.vogella.com/tutorials/Android/article.html

也不要快进,因为你可能会错过一些部分。: D

你的按钮也不会工作,因为你可能已经在你的MainActivity类上设置了视图,也初始化了按钮,但你没有在它上设置任何监听器。所以它看起来只是一个按钮,但根本不起作用。阅读下面我的评论:

public class MyActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Button button; //<-- You initialize your button
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); //<-- this layout shoud contain a button
    button = (Button) findViewById(R.id.YOUR_BUTTON_ID); //<-- locate your button based on the id from the main layout above
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //DO SOMETHING HERE let's say it's the method below:
            sendMessage(//but you need the parent view here or try using view or this but it will use this button as it's view as far as I know)
        }
    }); //<-- this whole part is the one that will handle the operation you will need once the button is clicked.
}
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = String.valueOf(editText.getText());
    intent.putExtra(EXTRA_MESSAGE, message);
}
}

您在sendMessage中创建了Intent,但是您忘记使用它启动活动。只需添加

startActivity(intent);

在函数的末尾,它应该工作(当然,如果你的布局文件是正确的)。

参见启动第二个活动

最新更新