[Android]Button、EditTextのサンプル

2021年9月1日

1.MainActivity.java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.buttondemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
/*EditText型変数*/
private EditText edText1,edText2,edText3;
/*Button型変数*/
private Button btnResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//関数addListenerOnButtonを呼び出す
addListenerOnButton();
}
private void addListenerOnButton() {
edText1 = (EditText)findViewById(R.id.edittext);
edText2 = (EditText)findViewById(R.id.edittext2);
edText3 = (EditText)findViewById(R.id.edittext3);
btnResult = (Button)findViewById(R.id.button1);
btnResult.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String tA = edText1.getText().toString();
String tB = edText2.getText().toString();
String tC = edText3.getText().toString();
//文字列を数値に変換
int res1 = Integer.parseInt(tA);
int res2 = Integer.parseInt(tB);
int res3 = Integer.parseInt(tC);
//結果を計算
int result = res1 + res2 + res3;
Toast.makeText(getApplicationContext(),
String.valueOf(result),Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/* アクションバーにアイテムが追加される*/
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.arkgame.buttondemo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { /*EditText型変数*/ private EditText edText1,edText2,edText3; /*Button型変数*/ private Button btnResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //関数addListenerOnButtonを呼び出す addListenerOnButton(); } private void addListenerOnButton() { edText1 = (EditText)findViewById(R.id.edittext); edText2 = (EditText)findViewById(R.id.edittext2); edText3 = (EditText)findViewById(R.id.edittext3); btnResult = (Button)findViewById(R.id.button1); btnResult.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { String tA = edText1.getText().toString(); String tB = edText2.getText().toString(); String tC = edText3.getText().toString(); //文字列を数値に変換 int res1 = Integer.parseInt(tA); int res2 = Integer.parseInt(tB); int res3 = Integer.parseInt(tC); //結果を計算 int result = res1 + res2 + res3; Toast.makeText(getApplicationContext(), String.valueOf(result),Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { /* アクションバーにアイテムが追加される*/ getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.arkgame.buttondemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
      /*EditText型変数*/
      private EditText edText1,edText2,edText3;
      /*Button型変数*/
      private Button btnResult;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
              //関数addListenerOnButtonを呼び出す
          addListenerOnButton();
      }

      private void addListenerOnButton() {

          edText1 = (EditText)findViewById(R.id.edittext);
          edText2 = (EditText)findViewById(R.id.edittext2);
          edText3 = (EditText)findViewById(R.id.edittext3);
            
          btnResult = (Button)findViewById(R.id.button1);
          btnResult.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
               String tA = edText1.getText().toString();
               String tB = edText2.getText().toString();
               String tC = edText3.getText().toString();		
                     
                     //文字列を数値に変換
               int res1 = Integer.parseInt(tA);
               int res2 = Integer.parseInt(tB);
               int res3 = Integer.parseInt(tC);
                     
                     //結果を計算
               int result = res1 + res2 + res3;
               Toast.makeText(getApplicationContext(),
               String.valueOf(result),Toast.LENGTH_LONG).show();
            }			
       });
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        /* アクションバーにアイテムが追加される*/
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
      }
}

2.activity_main.xml
属性
TextView:id、layout、textを指定
EditText:id、layout、inputType、textを指定
Button:id、layout、textを指定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example_button" />
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:ems="10"
android:inputType="text"
android:text="@string/enter_text1" />
<EditText
android:id="@+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edittext3"
android:layout_below="@+id/edittext"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="text"
android:text="@string/enter_text2" />
<EditText
android:id="@+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edittext"
android:layout_below="@+id/edittext2"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="text"
android:text="@string/enter_text3" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/edittext3"
android:layout_marginTop="35dp"
android:text="@string/click_button" />
</RelativeLayout>
<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/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/example_button" /> <EditText android:id="@+id/edittext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/textView1" android:layout_marginTop="61dp" android:ems="10" android:inputType="text" android:text="@string/enter_text1" /> <EditText android:id="@+id/edittext2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/edittext3" android:layout_below="@+id/edittext" android:layout_marginTop="17dp" android:ems="10" android:inputType="text" android:text="@string/enter_text2" /> <EditText android:id="@+id/edittext3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/edittext" android:layout_below="@+id/edittext2" android:layout_marginTop="14dp" android:ems="10" android:inputType="text" android:text="@string/enter_text3" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/edittext3" android:layout_marginTop="35dp" android:text="@string/click_button" /> </RelativeLayout>
<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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/example_button" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="61dp"
        android:ems="10"
        android:inputType="text"
        android:text="@string/enter_text1" />

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edittext3"
        android:layout_below="@+id/edittext"
        android:layout_marginTop="17dp"
        android:ems="10"
        android:inputType="text"
        android:text="@string/enter_text2" />

    <EditText
        android:id="@+id/edittext3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edittext"
        android:layout_below="@+id/edittext2"
        android:layout_marginTop="14dp"
        android:ems="10"
        android:inputType="text"
        android:text="@string/enter_text3" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/edittext3"
        android:layout_marginTop="35dp"
        android:text="@string/click_button" />

</RelativeLayout>

3.res/values/strings.xml 
定数属性
アプリ名前、入力項目名、ボタンの名

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GUIDemo</string>
<string name="action_settings">Settings</string>
<string name="example_button">Button 表示サンプル</string>
<string name="enter_text1"/>
<string name="enter_text2"/>
<string name="enter_text3"/>
<string name="click_button">数字計算</string>
</resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">GUIDemo</string> <string name="action_settings">Settings</string> <string name="example_button">Button 表示サンプル</string> <string name="enter_text1"/> <string name="enter_text2"/> <string name="enter_text3"/> <string name="click_button">数字計算</string> </resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">GUIDemo</string>
    <string name="action_settings">Settings</string>
    <string name="example_button">Button 表示サンプル</string>
    <string name="enter_text1"/>
    <string name="enter_text2"/>
    <string name="enter_text3"/>
    <string name="click_button">数字計算</string>

</resources>

4.AndroidManifest.xml
属性
android:icon アイコン
android:label ラベル
android:theme テーマ

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arkgame.guidemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.arkgame.guidemo.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>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.arkgame.guidemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.arkgame.guidemo.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>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.arkgame.guidemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.arkgame.guidemo.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>

 

Android

Posted by arkgame