【问题标题】:Simple Android Studio TextView Code Using Java使用 Java 的简单 Android Studio TextView 代码
【发布时间】:2015-06-17 15:21:28
【问题描述】:

我正在尝试使用他们参考中的代码在 Android Studio 中创建一个 TextView(我已经知道我可以使用 XML,但我正在尝试学习 java)。我的代码不起作用,我不知道为什么。我看起来就像参考上的代码。我试图弄清楚如何使用 Android 参考指南在 Android Studio 中编写代码。任何使用参考指南的提示和技巧都值得赞赏。

注意:请不要让 cmets 说我需要购买 java 书,参加 java 课程等,因为我已经这样做了。

我所指的参考资料 (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{

        }
    }

}

【问题讨论】:

  • 您收到了哪些错误(如果有)?
  • TextView extends MainActivity 似乎不对。您通常会创建一个TextView 并将其添加到活动布局中。但是现在,您的 TextView 实际上是 MainActivity 没有设置布局。

标签: java android android-studio


【解决方案1】:

如果我正确理解您要完成的工作,就是使用 java 代码在 Android 应用程序中创建一个 TextView。如果是这样,那么您以错误的方式接近它。让我解释一下:

你在做什么;您正在创建一个扩展(在面向对象编程词典中继承)TextView 的 Activity,然后使用 TextView 方法来更改小部件的外观。这意味着您将整个应用程序制作为单个 TextView 小部件,然后使用您预期的更改重载其方法,这不是平台设计的方式。

我认为你应该做什么;使用创建空白活动的 Android Studio 向导(如 http://developer.android.com/training/basics/firstapp/creating-project.html 所示)。删除“Hello World!”来自 activity_main.xml 的 TextView。在 MainActivity.java 中。您可以创建 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);
}

希望它对您有所帮助并为您指明正确的方向。

【讨论】:

    【解决方案2】:

    他们这样做是不必要的。这些方法已经内置在TextView 对象中。你不需要重建它们。这使你的工作加倍。只需实例化一个TextView 对象,然后调用这些方法。

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

    【讨论】:

      【解决方案3】:

      您只需在 java 中处理 textview 并声明它们并使用 java 设置文本,然后将 textview 放入布局中以向用户显示它们

      例如看:

      您有一个布局文件,例如main.xml 在那你添加TextView

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

      在java中你可以这样做

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

      并使用

      设置文本
       t.settext("your text")
      

      按承诺编辑

      您可以在TextViewExample 上有一个clicklistener

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

      还有一些 XML 属性可以帮助格式化
      考虑以下几点:

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

      你也可以动态地设置这个属性也可以使用java

      像这样:

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

      其他

      【讨论】:

      • 在布局中添加 textview 以便您查看
      猜你喜欢
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 2021-11-12
      • 2017-05-28
      • 2012-02-22
      • 1970-01-01
      相关资源
      最近更新 更多