简体   繁体   English

Java / Android的基本问题

[英]Basic issue with Java/Android

I started learning java a few days ago, so I'm quite lost with it. 几天前,我开始学习Java,因此我对此非常迷惑。 I want to show a text received from an intent and make it look like this: "You've written : ." 我想显示从一个意图接收到的文本,并使它看起来像这样:“您已经写了:”。 It isn't working at all and I'm only able to show the text from the intent. 它根本不起作用,我只能从意图中显示文本。

Here's my code: 这是我的代码:

// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
System.out.print("You've written: ");
textView.setText(message);
System.out.print(".");

// Set the text view as the activity layout
setContentView(textView);
}

Besides, I'm trying to display the text written above in the first line of the page (or as many lines it takes) and, below, a label to insert a text together with a Button. 此外,我正在尝试在页面的第一行(或需要的多行)中显示上方书写的文本,以及在下方显示用于将文本与Button一起插入的标签。 The problem is that I can only see the text from the intent. 问题是我只能从意图中看到文本。 Here's my code: 这是我的代码:

<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" />

    <EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        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"/>

</RelativeLayout>

Thankyou very much, I wish I'll be answered soon. 非常感谢,我希望尽快得到答复。

Instead of System.out.println. 而不是System.out.println。

Do like this. 这样吧

<TextView
    android:id="@+id/topTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

and Get the view in activity. 并获取活动视图。

public void onCreate(Bundle bundle) {
    .........
   setContentView(layout_xml);
   TextView textView = (TextView)findViewById(R.id.topTextView);
   textView.setText("You've written: " + message + " .");
   .........

}

If you are trying to reference the TextView inside your layout it needs to referenced in the Activity. 如果您尝试在布局内引用TextView,则需要在Activity中对其进行引用。 The above solution shows you how to handle that. 上面的解决方案显示了如何处理。 setContentView(R.layout.file_name) is what should be used to reference a layout created inside an xml file stored in res/layout/file_name.xml or created manually inside your code. setContentView(R.layout.file_name)是用来引用在存储在res / layout / file_name.xml中的xml文件中创建的布局或在代码内部手动创建的布局的内容。 If you are going to call setContentView for a layout contructed inside your (Activity) it will take some more code. 如果要为(Activity)内部构造的布局调用setContentView,则将需要更多代码。 If the message is all is need to be displayed you can always call Toast.maketText(Context, message, Toast.LENGTH_SHORT).show(); 如果只需要显示消息,则可以随时调用Toast.maketText(Context,message,Toast.LENGTH_SHORT).show();。 for example. 例如。 The other suggestion is Java developers are used to using System.out.println() to debug to the console. 另一个建议是Java开发人员习惯于使用System.out.println()调试控制台。 In Android they have a neat feature we use called LogCat that can display messages going on inside your code by saying (example for debugging purposes) Log.d(TAG, message); 在Android中,它们具有一项整洁的功能,我们使用称为LogCat的功能,它可以通过说出Log.d(TAG,message)(例如,用于调试目的的示例)来显示代码内部发生的消息。 Where TAG is usually a constant defined for the Activity,Fragment, etc. you are in so you can see where the message is coming from and display whatever value you normally would have used in System.out.println(). TAG通常是您为Activity,Fragment等定义的常量,因此您可以查看消息的来源并显示通常在System.out.println()中使用的任何值。

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

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