📜  Android中的TextView小部件(使用带有示例的Java)

📅  最后修改于: 2021-05-13 17:09:17             🧑  作者: Mango

窗口小部件是指UI(用户界面)的元素,可帮助用户与Android App进行交互。 TextView是许多此类小部件之一,可用于改善应用程序的UI。

TextView是指根据特定TextView的布局,大小,颜色等设置的在屏幕上显示一些文本的小部件。它也可以让我们修改或编辑自己。

类语法:

public class TextView
  extends View 
  implements ViewTreeObserver.OnPreDrawListener

类层次结构:

java.lang.Object
  ↳ android.view.View
     ↳ android.widget.TextView

句法:


    .
    .
    
        android:SomeAttribute1 = "Value of attribute1"
        android:SomeAttribute2 = "Value of attribute2"
        .
        .
        android:SomeAttributeN = "Value of attributeN"
    
    .
    .

这里的布局可以是任何布局,例如相对,线性等(请参阅本文以了解有关布局的更多信息)。在本文下面给出的表中,属性可能很多。

例子:



    

 

如何在Android应用程序中包含TextView:

  1. 首先,创建一个新的Android应用程序,或使用一个现有的应用程序对其进行编辑。在两种情况下,都必须有一个XML布局活动文件和一个与此活动链接的Java类文件。
  2. 打开活动文件,并在该文件中包含一个TextView。 TextView的代码将是:
  3. 现在在Java文件中,将此布局文件与以下代码链接:
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    

    其中activity_main是要附加的布局文件的名称。

  4. 在Java文件中,我们将尝试在触摸Toast消息时更改TextView上显示的Text。
  5. 下面给出了布局文件和Java文件的完整代码。

下面是上述方法的实现:

activity_main.xml

  

  
    
  


MainActivity.java
package com.project.textview;
  
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    // Creating the instance of the TextView created
    private TextView textView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
  
        // Linking the activity file to this Java file
        setContentView(R.layout.activity_main);
  
        // Get the TextView with the id
        // mentioned in the layout file
        textView = (TextView)findViewById(R.id.textview);
  
        // Try to change the text of the Textview upon touch
        // and also display a Toast message
        textView.setOnClickListener(new View.OnClickListener() {
  
            @Override
            public void onClick(View v)
            {
  
                // Changing text
                textView.setText("GeeksforGeeks");
  
                // Displaying Toast message
                Toast
                    .makeText(MainActivity.this,
                              "Welcome to GeeksforGeeks",
                              Toast.LENGTH_SHORT)
                    .show();
            }
        });
    }
}


输出:

Android中TextView的XML属性

Attributes Description
android:text Sets text of the Textview
android:id Gives a unique ID to the Textview
android:cursorVisible Use this attribute to make cursor visible or invisible. Default value is visible.
android:drawableBottom Sets images or other graphic assets to below of the Textview.
android:drawableEnd Sets images or other graphic assets to end of Textview.
android:drawableLeft Sets images or other graphic assets to left of Textview.
android:drawablePadding Sets padding to the drawable(images or other graphic assets) in the Textview. android:autoLink This attribute is used to automatically detect url or emails and show it as clickable link.
android:autoText Automatically correct spelling errors in text of the Textview.
android:capitalize It automatically capitalize whatever the user types in the Textview.
android:drawableRight Sets drawables to right of text in the Textview.
android:drawableStart Sets drawables to start of text in the Textview.
android:drawableTop Sets drawables to top of text in the Textview.
android:ellipsize Use this attribute when you want text to be ellipsized if it is longer than the Textview width.
android:ems Sets width of the Textview in ems.
android:gravity We can align text of the Textview vertically or horizontally or both.
android:height Use to set height of the Textview.
android:hint Use to show hint when there is no text.
android:inputType Use to set input type of the Textview. It can be Number, Password, Phone etc.
android:lines Use to set height of the Textview by number of lines.
android:maxHeight Sets maximum height of the Textview.
android:minHeight Sets minimum height of the Textview.
android:maxLength Sets maximum character length of the Textview.
android:maxLines Sets maximum lines Textview can have.
android:minLines Sets minimum lines Textview can have.
android:maxWidth Sets maximum width Textview can have.
android:minWidth Sets minimum lines Textview can have.
android:textAllCaps Show all texts of the Textview in capital letters.
android:textColor Sets color of the text.
android:textSize Sets font size of the text.
android:textStyle Sets style of the text. For example, bold, italic, bolditalic.
android:typeface Sets typeface or font of the text. For example, normal, sans, serif etc
android:width Sets width of the TextView.