P114.複数のウィジェットを配置する

Android開発に関するメモランダム

カテゴリー: Android Studio ではじめる Android プログラミング入門 第3版 Android Studio 2対応  閲覧数:340 配信日:2018-03-18 11:37


▼L:\Android\AndroidStudioProject\App20180302\app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="tokyo.w3c.android0.app20180302.MainActivity">

   <LinearLayout
       android:layout_width="368dp"
       android:layout_height="495dp"
       android:orientation="vertical"
       tools:layout_editor_absoluteX="8dp"
       tools:layout_editor_absoluteY="8dp">

       <TextView
           android:id="@+id/textView"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="TextView"
           android:textSize="36sp"
           tools:text="Your name:" />

       <EditText
           android:id="@+id/editText"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:ems="10"
           android:inputType="textPersonName"
           android:text="Name"
           android:textSize="24sp" />

       <Button
           android:id="@+id/button"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:onClick="button_onClick"
           android:text="Button"
           android:textSize="24sp"
           tools:text="Click" />
   </LinearLayout>
</android.support.constraint.ConstraintLayout>


FrameLayoutで囲うのを忘れていたので、最初からやり直した
▼L:\Android\AndroidStudioProject\App20180302\app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="tokyo.w3c.android0.app20180302.MainActivity">


   <FrameLayout
       android:layout_width="368dp"
       android:layout_height="495dp"
       tools:layout_editor_absoluteX="8dp"
       tools:layout_editor_absoluteY="8dp">

       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical">

           <TextView
               android:id="@+id/textView"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="TextView"
               android:textSize="36sp"
               tools:text="Your name:" />

           <EditText
               android:id="@+id/editText"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:ems="10"
               android:inputType="textPersonName"
               android:text="Name"
               android:textSize="24sp" />

           <Button
               android:id="@+id/button"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:onClick="button_onClick"
               android:text="Button"
               tools:text="Click" />
       </LinearLayout>
   </FrameLayout>
</android.support.constraint.ConstraintLayout>


EditText
・テキストを入力するためのウィジェット
<EditText
               android:id="@+id/editText2"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:ems="10"
               android:inputType="textPersonName"
               android:text="Name"
               android:textSize="24sp" />


<Button  android:onClick="button_onClick" />
・「ボタンをタップすると、button_onClickメソッドを実行する」という設定を行っている


書き方は3種類



MainActivity.java
▼L:\Android\AndroidStudioProject\App20180302\app\src\main\java\tokyo\w3c\android0\app20180302\MainActivity.java

(TextView)this.findViewById(R.id.textView); / (EditText)this.findViewById(R.id.editText);
package tokyo.w3c.android0.app20180302;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }

   public void button_onClick(View view) {
       TextView text = (TextView)this.findViewById(R.id.textView);
       EditText edit = (EditText)this.findViewById(R.id.editText);
       Editable s = edit.getText();
       text.setText("Hi," + s + "!");
   }
}


(TextView)findViewById(R.id.textView); / (EditText)findViewById(R.id.editText);
package tokyo.w3c.android0.app20180302;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }

   public void button_onClick(View view) {
       TextView text = (TextView)findViewById(R.id.textView);
       EditText edit = (EditText)findViewById(R.id.editText);
       Editable s = edit.getText();
       text.setText("Hi," + s + "!");
   }
}


findViewById(R.id.textView); / findViewById(R.id.editText);
package tokyo.w3c.android0.app20180302;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }

   public void button_onClick(View view) {
       TextView text = findViewById(R.id.textView);
       EditText edit = findViewById(R.id.editText);
       Editable s = edit.getText();
       text.setText("Hi," + s + "!");
   }
}








Android StudioではじめるAndroidプログラミング入門 第3版 Android Studio 2対応

週間人気ページランキング / 2-16 → 2-22
順位 ページタイトル抜粋 アクセス数
アクセスが、ありませんでした! 0
2025/2/23 1:01 更新
指定期間人気ページランキング / 1970-1-1 → 2025-2-22
順位 ページタイトル抜粋 アクセス数
アクセスが、ありませんでした! 0
2025/2/23 1:01 更新