0

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

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

视图写在 xml 文件中,与视图相关的活动在 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 它是由 eclipse 自动创建的。我没有改变它。

<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>
4

4 回答 4

2

发布您的完整代码 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 文件中的 int 唯一 ID 访问编辑 EditText。

于 2013-08-13T12:46:25.327 回答
1

为了回答你的问题...

R.id.edit_message是一个整数,由 Android 生成,它引用布局文件中设置的 id,因此,假设您的布局 XML 包含类似

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

thenR.id.edit_message是一个标识符,它告诉 Android,“当我使用这个布局和这个 id 时,我的意思是那个特定的 EditText 而不是其他的”。EditText 是您在其中键入消息的框。据推测,它的内容正在通过 editText.getText().toString() 读取以传递给显示活动。

com.example.myfirstapp.MESSAGE只是一个键的名称。启动活动时,您可以以键/值对的形式向其发送额外的数据,例如,

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(EXTRA_MESSAGE, "Your message")
startActivity(intent);

将密钥声明为公共静态字段意味着您可以在一个活动中使用密钥,然后从另一个活动中获取密钥名称以检索密钥,这就是

intent.getStringExtra(MainActivity.EXTRA_MESSAGE)

是在做。

setContentView用于告诉活动它应该使用哪个布局文件*,并且应该在任何需要访问这些视图的代码之前在 onCreate 中调用,例如,读取 EditText 的内容,或将点击处理程序添加到按钮等。


*实际上,它不一定是对布局文件的引用。您可以先创建自己的视图,在这种情况下,需要在创建所有视图后调用它。


为了回答你的问题...

在您的显示活动中,您正在创建一个新的 TextView 对象并设置其消息,但您没有将该视图添加到布局中,因此您永远看不到它。相反,您会看到已经存在的文本视图(其中包含 hello world 消息)。要更改现有 TextView 的消息,请activity_display_message.xml给它一个 ID,例如

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

然后做

    setContentView(R.layout.activity_display_message);
    TextView textView = (TextView) findViewById(R.id.display_message);
    textView.setTextSize(40);
    textView.setText(message);

更改其内容。(如果你真的想添加一个新的textview,你需要给activity_display_message.xml中的RelativeLayout一个id,然后做类似的事情

    // set the layout file:
    setContentView(R.layout.activity_display_message);

    // get a reference to the layout:
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.display_layout);

    // create your new textview:
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // set the TextView to display top left:
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

    // add the view to layout:
    rl.addView(textView, params);
于 2013-08-13T12:40:48.567 回答
0

对于你的问题 #2:

您的常量将保存在 res/values/strings.xml 中。这样做是因为如果您需要本地化您的应用程序,您可以只用另一种语言构建另一组 string.xml。因此,如果您通过 layout-xml-file 为 TextView 之类的内容分配值,则可以使用

android:text="CONSTANT_NAME"
于 2013-08-13T12:34:01.377 回答
0

答案 1) EditText 是一个简单的 TextView,它允许用户修改其内容。

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

在这个类文件中,这基本上是对在 XML 中创建的 EditText 的引用,因此您可以通过更改其内容或查看用户键入的内容与它进行交互。

R.id.edit_message

如果您查看 xml,它将被分配一个 ID,通常类似于 @+id/edit_message。这就是代码知道您要附加到哪个视图的方式。

com.example.myfirstapp.MESSAGE

这是引用通过 Intent 传入的字符串的一种方法。

setContentView(R.layout.activity_display_message);

一旦您完成了视图的构建,就应该调用它。

Why is it displaying always "Hello World!" all the time?

因为听起来你永远不会改变它。查看 strings.xml 文件并更改“Hello World!”的值,这将更改应用程序中的值。

于 2013-08-13T12:39:42.010 回答