2

我今天安装了 Android SDK 包,我正在遵循“我的第一个应用程序”教程,但我被卡住了,它指出:

打开 MainActivity 类(位于项目的 src/ 目录下)并添加相应的方法:

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
}

我在哪里把它放在文件中?这是“MainActivity.java”文件吗?

我已经尝试过并且不断收到错误,所以我显然在某个地方出错了。

活动主.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />
</LinearLayout>

MainActivity.java

    package com.example.myfirstapp;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;



public class MainActivity extends Activity {

    @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;
    }

}

希望我已经把我的问题说清楚了,我在论坛上寻找答案,但我找不到任何东西。

4

3 回答 3

3

如果您的(比如说activity_main.xml)xml 布局中有一个按钮,并且您有以下按钮属性

 android:onClick="sendMessage"

你有以下内容MainActiivty.java

 setContentView(R.layout.activity_main); 

你应该有以下内容MainActivity.java

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
}

例子:

MainActivity.java

// Your imports
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //setting the layout to activity
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">

    // other widgets
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="146dp"
        android:onClick="sendMessage"
        android:text="Button" />

</RelativeLayout>
于 2013-10-12T14:04:37.603 回答
0

如果您是一个新的 android 开发人员并且是第一次做,那么从基本的开始,比如启动新的活动,它包含 hello world 或任何文本视图,按钮,那么您将清楚地了解应用程序。

  1. 创建你的安卓应用
  2. 在 XML 布局中拖动按钮和文本视图
  3. 运行您的第一个应用程序。
  4. 你会得到你的输出。
于 2013-10-12T14:21:18.267 回答
0

把它放在 MainActivity.java 之后的右上方

public class MainActivity extends ActionBarActivity {

完成此操作后,您可能需要导入。通过按 control / shift / O (非零)来执行此操作

于 2014-05-26T17:56:11.557 回答