简体   繁体   English

ID值资源:Android Studio

[英]ID Value Resource: Android Studio

https://developer.android.com/training/basics/firstapp/starting-activity#BuildIntent https://developer.android.com/training/basics/firstapp/starting-activity#BuildIntent

I am following along the demo on this website to gain a general idea of how Android Studio works.我正在关注本网站上的演示,以大致了解 Android Studio 的工作原理。 For the following portion of the code, it says to "press Alt+Enter" to clear errors.对于代码的以下部分,它说“按 Alt+Enter”以清除错误。 The problem is that the editText in line 3 ( R.id.editText ) is making me create a new id value resource or renaming the reference (which I do not want to do).问题是第 3 行( R.id.editText )中的 editText 正在让我创建一个新的 id 值资源或重命名引用(我不想这样做)。

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

Can someone explain what the id value resource is?有人可以解释一下 id 值资源是什么吗? Also, suggestions on how to fix this issue?另外,有关如何解决此问题的建议? I am new to Android Studio, so I do not understand what exactly this means.我是 Android Studio 的新手,所以我不明白这到底是什么意思。

Thank you.谢谢你。 it says "error: cannot find symbol"它说“错误:找不到符号”

To solve your issue, i suppose in your activity main layout you have added an editText as view, this is how to add Editext into your layout为了解决您的问题,我想在您的活动主布局中您添加了一个 editText 作为视图,这就是如何将 Edittext 添加到您的布局中

<EditText 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editTextTextPersonName" //this is the id value
    />

In your activity code set the id this way在您的活动代码中以这种方式设置 id

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editTextTextPersonName);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

Id of a resource is uniq identifier, such as name of a variable.资源的 id 是 uniq 标识符,例如变量的名称。 It should be uniq or you will have problems with names.它应该是 uniq ,否则你会遇到名称问题。

Xml file where it write: 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="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">

<!--LOOOOOOOOOOOOOK HERE-->
<TextView
    android:id="@+id/placeHereIdWhichYouWant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
/>

</LinearLayout>

Your string editText.getText().toString();你的字符串editText.getText().toString(); just take text from your EditText View.只需从您的EditText视图中获取文本。 Just give the id for EditText and all will work, if it doesn't import R.只需提供 EditText 的 id,如果它不导入 R,一切都会正常工作。 to your class到您的 class

package com.example.test //here name of your package

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM