Android-textview-control

提供:Dev Guides
移動先:案内検索

Android-TextViewコントロール

'_TextView はテキストをユーザーに表示し、オプションでユーザーが編集できるようにします。 TextViewは完全なテキストエディターですが、基本クラスは編集を許可しないように構成されています。_

TextView属性

以下は、TextViewコントロールに関連する重要な属性です。 属性の完全なリストと、これらの属性を実行時に変更するために使用できる関連メソッドについては、Androidの公式ドキュメントを確認してください。

Sr.No. Attribute & Description
1

android:id

これは、コントロールを一意に識別するIDです。

2

android:capitalize

設定されている場合、このTextViewにテキスト入力メソッドがあり、ユーザーが入力したものを自動的に大文字にする必要があることを指定します。

  • 自動的に何も大文字にしない-0
  • 各文の最初の単語を大文字にします-1
  • すべての単語の最初の文字を大文字にします-2 *すべての文字を大文字にします-3
3
  • android:cursorVisible*

カーソルを表示(デフォルト)または非表示にします。 デフォルトはfalseです。

4

android:editable

trueに設定されている場合、このTextViewに入力メソッドがあることを指定します。

5

android:fontFamily

テキストのフォントファミリ(名前は文字列)。

6

android:gravity

テキストがビューよりも小さい場合に、ビューのx軸またはy軸、あるいはその両方でテキストを配置する方法を指定します。

7

android:hint

テキストが空のときに表示するヒントテキスト。

8

android:inputType

テキストフィールドに配置されるデータのタイプ。 電話、日付、時刻、番号、パスワードなど

9

android:maxHeight

TextViewの高さを最大でこのピクセル数にします。

10

android:maxWidth

TextViewの幅を最大でこのピクセル数にします。

11

android:minHeight

TextViewの高さを少なくともこのピクセル数にします。

12

android:minWidth

TextViewを少なくともこの数ピクセル幅にします。

13

android:password

フィールドの文字をそれ自体ではなくパスワードのドットとして表示するかどうか。 可能な値は「true」または「false」です。

14

android:phoneNumber

設定されている場合、このTextViewに電話番号入力メソッドがあることを指定します。 可能な値は「true」または「false」です。

15

android:text

表示するテキスト。

16

android:textAllCaps

すべて大文字でテキストを提示します。 可能な値は「true」または「false」です。

17

android:textColor

テキストの色。 「#rgb」、「#argb」、「#rrggbb」、または「#aarrggbb」の形式の色の値を指定できます。

18

android:textColorHighlight

テキスト選択ハイライトの色。

19

android:textColorHint

ヒントテキストの色。 「#rgb」、「#argb」、「#rrggbb」、または「#aarrggbb」の形式の色の値を指定できます。

20

android:textIsSelectable

編集不可能なテキストのコンテンツを選択できることを示します。 可能な値は「true」または「false」です。

21

android:textSize

テキストのサイズ。 テキストの推奨されるディメンションタイプは、スケーリングされたピクセルの「sp」です(例:15sp)。

22

android:textStyle

テキストのスタイル(太字、斜体、太字)。 「

」で区切られた以下の値を1つ以上使用できます。

  • 通常-0 * 太字-1 *斜体-2
23
  • android:typeface*

テキストの書体(通常、サン、セリフ、固定幅)。 「

」で区切られた以下の値を1つ以上使用できます。

  • 通常-0 * サンズ-1 * セリフ-2 * 等幅-3

この例では、簡単な手順で、リニアレイアウトとTextViewを使用して独自のAndroidアプリケーションを作成する方法を示します。

Step Description
1 You will use Android studio to create an Android application and name it as demo under a package com.example.demo as explained in the Hello World Example chapter.
2 Modify src/MainActivity.java file to add necessary code .
2 Modify the default content of res/layout/activity_main.xml file to include Android UI control.
3 No need to change default string constants at string.xml file. Android studio takes care of default string constants.
4 Run the application to launch Android emulator and verify the result of the changes done in the application.

以下は、変更されたメインアクティビティファイル src/com.example.demo/MainActivity.java の内容です。 このファイルには、基本的な各ライフサイクルメソッドを含めることができます。

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

     //--- text view---
      TextView txtView = (TextView) findViewById(R.id.text_id);
   }
}

以下は res/layout/activity_main.xml ファイルの内容です-

<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=".MainActivity" >

   <TextView
      android:id="@+id/text_id"
      android:layout_width="300dp"
      android:layout_height="200dp"
      android:capitalize="characters"
      android:text="hello_world"
      android:textColor="@android:color/holo_blue_dark"
      android:textColorHighlight="@android:color/primary_text_dark"
      android:layout_centerVertical="true"
      android:layout_alignParentEnd="true"
      android:textSize="50dp"/>

</RelativeLayout>

以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">demo</string>
</resources>

以下は、 AndroidManifest.xml のデフォルトのコンテンツです-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.demo" >


   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme" >

      <activity
         android:name="com.example.demo.MainActivity"
         android:label="@string/app_name" >

         <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>

      </activity>

   </application>
</manifest>
*demo* アプリケーションを実行してみましょう。 環境設定中に *AVD* を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-

Android textView Control

運動

TextViewのルックアンドフィールを変えるために、プログラミング時にレイアウトXMLファイルのTextViewのさまざまな属性を使用して上記の例を試すことをお勧めします。 編集可能にし、フォントの色、フォントファミリ、幅、textSizeなどに変更して、結果を確認してください。 1つのアクティビティで複数のTextViewコントロールを使用して上記の例を試すこともできます。