简体   繁体   English

使用Java的简单Android Studio TextView代码

[英]Simple Android Studio TextView Code Using Java

I am trying to create a TextView in Android Studio using the code on their reference (I already know I can use XML but I am trying to learn java). 我正在尝试使用他们参考上的代码在Android Studio中创建TextView(我已经知道我可以使用XML,但是我正在尝试学习Java)。 My code doesn't work and I can't figure out why. 我的代码不起作用,我也不知道为什么。 I look just like the code on the reference. 我看起来就像参考上的代码。 I am trying to figure out how to use the Android reference guide to code in Android Studio. 我试图弄清楚如何使用Android参考指南在Android Studio中进行编码。 Any tips and tricks for using the reference guide are appreciated too. 使用参考指南的任何提示和技巧也将受到赞赏。

Note: Please no comments saying I need to buy a java book, take a java class, etc because I am already doing that. 注意:请不要发表任何评论,因为我已经在这样做,所以我需要购买Java书籍,参加Java课程等。

The reference I am referring to ( http://developer.android.com/reference/android/widget/TextView.html# ) 我要参考的参考( http://developer.android.com/reference/android/widget/TextView.html#

    //(class) Adds TextView
public class TextView extends MainActivity{
    CharSequence myText = "Marsha Jackson - (555) 555-5555 - marsha.jackson@email.com - www.jkl.com";

    //(method) Sets width for the textview
    public void setWidth (int pixels){
        int = 100;
    }

    //(method) Sets the height of the TextView
    public void setHeight (int pixels){
        int = 500;
    }
    //(method) Sets the size of the text
    public void setTextSize (float size){
        float = 40;

    }

    //Sets the typeface fo the font
    public void setTypeface (Typeface tf) {
        Typeface Arial;
    }
    //(method) Sets the text color
    public void setTextColor (ColorStateList colors){
        ColorStateList Red;
    }

    //(method) Sets the colors for links
    public final void setLinkTextColor (int color) {
        int Yellow;
    }

    //(method)Sets highlighted text color
    public void setHighlightColor (int color){
        int Green;
    }

    //(method) Sets text to be ellipsized
    public void setEllipsize (TextUtils.TruncateAt where){
        Enum END;
    }

    //(method) Sets text
    public final void setText(CharSequence text){
        myText = text;
    }
    //(method)Makes text selectable
    public void setTextIsSelectable (boolean selectable){

    }

    //(method) Return the state of the textIsSelectable flag
    public boolean isTextSelectable(){
        return (setTextIsSelectable);

    }
    //(Method) Lets user select websites, phone numbers, and emails
    public final void setAutoLinkMask (int mask){
        public static final int all{

        }
        //(Field)Filters out numbers that are too short to be phone numbers
        public static final Linkify.MatchFilter sPhoneNumberMatchFilter{

        }
        //(Field)Filters out symbols that can be in phone numbers
        public static final Linkify.TransformFilter sPhoneNumberTransformFilter{

        }
        //(Field) Prevent text after @ sign from becoming a website link
        public static final Linkify.MatchFilter sUrlMatchFilter{

        }
    }

}

if I understand correctly what you're trying to accomplish, is with java code create a TextView in an Android App. 如果我正确理解您要完成的工作,请使用Java代码在Android应用程序中创建TextView。 If so, you are aproaching it the wrong way. 如果是这样,则说明您的方法是错误的。 Let me explain: 让我解释:

What you are doing; 你在做什么; You are creating an Activity that extends (inherits in Object Oriented Programming lexicon) TextView, and then use TextView methods to change the appearance of the widget. 您正在创建一个扩展(继承于“面向对象的程序”词典中的)TextView的Activity,然后使用TextView方法更改小部件的外观。 What this means is that you are making the whole app a single TextView Widget and then overloading its methods with your intended changes which is not the way the platform was designed. 这意味着您要使整个应用程序成为一个单独的TextView窗口小部件,然后使用预期的更改来重载其方法,而这并不是平台设计的方式。

What I think you should do; 我认为你应该做什么; use the Android Studio wizard that creates a blank activity (as shown in http://developer.android.com/training/basics/firstapp/creating-project.html ). 使用可创建空白活动的Android Studio向导(如http://developer.android.com/training/basics/firstapp/creating-project.html中所示)。 Remove the "Hello World!" 删除“ Hello World!” TextView from activity_main.xml. 来自activity_main.xml的TextView。 In MainActivity.java. 在MainActivity.java中。 you can create the TextView and fill it out inside the onCreate method with code like this: 您可以创建TextView并使用以下代码将其填充到onCreate方法中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView myTextView = new TextView(this);
    myTextView.setText("Marsha Jackson - (555) 555-5555 - marsha.jackson@email.com - www.jkl.com");
    myTextView.setWidth(100);
    myTextView.setHeight(500);
    //you can keep adding code to change myTextView
    setContentView(myTextView);
}

Hope that it helps and sets you in the right direction. 希望它能帮助您并朝正确的方向发展。

You just handle the textview in java you declare them and use java to set the text and you put textview in your layout to show them to user 您只需在Java中处理textview,然后声明它们并使用java设置文本,然后将textview放在布局中即可向用户显示

For example see : 例如看:

You have a layout file such as main.xml In that you add TextView 您有一个布局文件(例如main.xml在其中添加了TextView

<TextView
    android:id=@+id/text
  />

And in java you do this 在Java中,您可以执行此操作

 TextView t = (TextView) findViewById(R.id.text);

And set text using 并使用设置文字

 t.settext("your text")

Edit as promised 按承诺编辑

You can have a clicklistener on TextView Example : 您可以在TextView示例上使用clicklistener

text2.setOnClickListener(newView.OnClickListener() {    
    public voidonClick(View v) {    
   Toast.makeText(getApplicationContext(),"Text was clicked",    
   Toast.LENGTH_LONG).show();

And there are also some XML attributes to help in formating 还有一些XML属性可以帮助格式化
For it consider the following: 为此,请考虑以下事项:

<TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/text1"

        android:text="@string/hello_world"

        android:textStyle="bold"

        android:textColor="#ff00ff"

        android:background="#00ff00"

        android:textColorHighlight="#000000"

        android:textIsSelectable="true"/>

You can also set this attributes dynamically also using java 您也可以使用Java 动态设置此属性

Like this : 像这样 :

Text.settext(" ");
Text.setheight(" ");

And other 和别的

They way that you are doing it is unnecessary. 他们认为您这样做是不必要的。 These methods are already built into the TextView object. 这些方法已经内置在TextView对象中。 You don't need to rebuild them. 您不需要重建它们。 This is doubling your work. 这使您的工作加倍。 Just instantiate a TextView object and then call those methods. 只需实例化TextView对象,然后调用这些方法。

// access textview that is in xml layout file
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Hello");
textView.setTextHeight(45);
....

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

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