我正在尝试使用他们的参考代码在Android Studio中创建一个TextView(我已经知道我可以使用XML但我正在尝试学习java)。我的代码不起作用,我无法弄清楚原因。我看起来就像参考上的代码。我想弄清楚如何使用Android参考指南在Android Studio中编码。任何使用参考指南的提示和技巧也值得赞赏。
注意:请不要评论说我需要购买一本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{
}
}
}
答案 0 :(得分:3)
如果我理解你正在努力实现的目标,那么使用java代码在Android应用程序中创建一个TextView。如果是这样,你就是以错误的方式接受它。让我解释一下:
你在做什么;您正在创建一个扩展的Activity(在面向对象的编程词典中继承)TextView,然后使用TextView方法更改窗口小部件的外观。这意味着您将整个应用程序作为单个TextView小部件,然后使用您预期的更改重载其方法,这不是平台的设计方式。
我认为你应该做的;使用创建空白活动的Android Studio向导(如this question所示)。删除“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);
}
希望它能帮助您并使您朝着正确的方向前进。
答案 1 :(得分:1)
您只需处理java中的textview,并声明它们并使用java设置文本,并将textview放在布局中以向用户显示
例如见:
您有一个布局文件,例如main.xml
您可以添加TextView
<TextView
android:id=@+id/text
/>
在java中你可以这样做
TextView t = (TextView) findViewById(R.id.text);
使用
设置文字 t.settext("your text")
按照承诺编辑
您可以clicklistener
TextView
示例:
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(" ");
和其他
答案 2 :(得分:1)
他们这样做是不必要的。这些方法已内置于TextView
对象中。您不需要重建它们。这使你的工作倍增。只需实例化一个TextView
对象,然后调用这些方法。
// access textview that is in xml layout file
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Hello");
textView.setTextHeight(45);
....