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

Java Android应用程序开发-基本问题,java,android,eclipse,Java,Android,Eclipse,我是一名PHP开发人员。今天我试着学习如何开发android应用程序。我从Hello World应用程序开始 看来我做错了什么。无论我在文本框中键入什么,我只会在下一个视图中得到“Hello World!”作为输出 视图写在xml文件中,与视图相关的活动在java类中 sendMessage() 此函数包含 EditText editText = (EditText) findViewById(R.id.edit_message); 1) 什么是编辑文本?在xml中,我可以看到它正在创建一

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

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

视图写在xml文件中,与视图相关的活动在java类中

sendMessage()
此函数包含

 EditText editText = (EditText) findViewById(R.id.edit_message);
1) 什么是编辑文本?在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

最后,为什么它总是显示“你好,世界!”呢

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

代码

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

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

}
MainActivity.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);
    }

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

}
活动显示消息.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>

strings.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>

关于您的问题#2:

常量将保存在res/values/strings.xml中。之所以这样做,是因为如果您需要本地化应用程序,您可以用另一种语言构建另一组string.xml。因此,如果您通过布局xml文件为TextView之类的对象指定了一个值,则可以使用

android:text="CONSTANT_NAME"
关于你的问题#2:

常量将保存在res/values/strings.xml中。之所以这样做,是因为如果您需要本地化应用程序,您可以用另一种语言构建另一组string.xml。因此,如果您通过布局xml文件为TextView之类的对象指定了一个值,则可以使用

android:text="CONSTANT_NAME"
回答1)EditText是一个简单的文本视图,允许用户修改其内容

EditText editText = (EditText) findViewById(R.id.edit_message);
在这个类文件中,这基本上是对用XML创建的EditText的引用,因此您可以通过更改其内容或查看用户键入的内容来与之交互

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

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!”的值,这将更改应用程序中的值。

答案1)EditText是一个简单的文本视图,允许用户修改其内容

EditText editText = (EditText) findViewById(R.id.edit_message);
在这个类文件中,这基本上是对用XML创建的EditText的引用,因此您可以通过更改其内容或查看用户键入的内容来与之交互

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

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!”的值,这将更改应用程序中的值。

要回答您的问题…

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

<EditText android:id="@+id/edit_message" ... />
    // 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);
将密钥声明为公共静态字段意味着您可以在一个活动中使用密钥,然后从另一个活动中获取密钥名称以检索密钥,这就是

intent.getStringExtra(MainActivity.EXTRA_MESSAGE)
他正在做什么

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


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


回答您的问题…

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

<TextView
        android:id="@+id/display_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
改变它的内容。(如果确实要添加新的textview,则需要为activity_display_message.xml中的RelativeLayout提供一个id,然后执行以下操作:

<EditText android:id="@+id/edit_message" ... />
    // 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);

回答您的问题…

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

<EditText android:id="@+id/edit_message" ... />
    // 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);
将密钥声明为公共静态字段意味着您可以在一个活动中使用密钥,然后从另一个活动中获取密钥名称以检索密钥,这就是

intent.getStringExtra(MainActivity.EXTRA_MESSAGE)
他正在做什么

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


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


回答你