java - Android 应用程序开发 - 基本问题

标签 java android eclipse

我是一名 PHP 开发人员。今天我尝试学习如何开发Android应用程序。我从 Hello World 应用程序开始。

看来我做错了什么。无论我在文本框中输入什么,我都只会得到“Hello World!”作为下一个 View 中的输出。

View 编写在 xml 文件中,与 View 相关的 Activity 在 java 类中。

sendMessage()

该函数包含

 EditText editText = (EditText) findViewById(R.id.edit_message);

1) 什么是EditText?在 xml 中,我可以看到它正在创建一个要输入的文本框。它在类文件中如何工作?和“R.id.edit_message”里面会有什么值?它是一个对象吗?

2) public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

我对“com.example.myfirstapp.MESSAGE”一无所知;信息。它是什么?它从哪里来?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Show the Up button in the action bar.
        setupActionBar();
    }

setContentView(R.layout.activity_display_message); -> 我们什么时候应该调用setContentView?

最后,为什么总是显示“Hello World!”一直吗?

我知道,看到这样的问题会令人沮丧。 但我确实需要从基础开始了解开发。 原谅我!

代码

DisplayMessageActivity.java

package com.example.myfristapp;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

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

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(R.layout.activity_display_message);

        // Show the Up button in the action bar.
        setupActionBar();
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

MainActivity.java

package com.example.myfristapp;

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 final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @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;
    }

    /** Called when the user clicks 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);
        startActivity(intent);
    }

}

activity_display_message.xml 它是由 eclispse 自动创建的。我没有改变它。

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DisplayMessageActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_display_message">My Message</string>
    <string name="hello_world">Hello world!</string>

</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

最佳答案

发布您的完整代码 xml 和 java 文件。

以及关于 R.id.edit_message

当你放

  <EditText
        android:id="@+id/edit_message"
....>

    </EditText>

在 xml 文件中,android 会为 R.java 文件中的控件 edit_message 生成一个唯一的 id 从这里你可以访问java文件中的任何控件

因此,当您使用 R.id.edit_message 时,这意味着您可以从 java 类中的 R.java 文件中访问编辑 EditText int 的唯一 id。

关于java - Android 应用程序开发 - 基本问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18208983/

相关文章:

java - 如何在 Android 中重试上传到服务器

android - 我的应用程序无法在 API 23 中运行

c++ - 不能在 C++ 中使用宏

android - 请问android :supportsRtl = false will make my application non-compatible for rtl enable devices

c++ - 如何使用索引交换预定义的数组元素?

java - Google Maps Android API v2 和 Eclipse,无法运行

java - JTabbedPane如何判断用户离开的tab?

java - Java 中泛型的访问类型

java - Tomcat 属性配置文件位置

java - Maven 将 native 添加到库路径