LoginSignup
2
5

More than 5 years have passed since last update.

Android Studioサンプルプログラム

Posted at

Android サンプルプログラム

ログイン画面
スクリーンショット 2017-11-12 3.39.25.png

ログイン成功の画面
スクリーンショット 2017-11-12 3.38.05.png

ログイン失敗
スクリーンショット 2017-11-12 3.39.55.png

スクリーン2画面
スクリーンショット 2017-11-12 3.40.14.png

MainActivity
package com.example.non.myapplication;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

        final EditText input_user =(EditText) findViewById(R.id.input_user_id);
        final EditText input_password =(EditText) findViewById(R.id.input_password);
        final Button login_button = (Button) findViewById(R.id.login_button);
        final Button screen_button = (Button) findViewById(R.id.screen_two_button);

        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        login_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(input_user.getText().toString().equals("kapibara") && input_password.getText().toString().equals("san")){
                    Intent intent = new Intent(getApplication(), SubScreen.class);
                    startActivity(intent);
                }else{
                    // アラートダイアログのタイトルを設定します
                    alertDialogBuilder.setTitle("エラー");
                    // アラートダイアログのメッセージを設定します
                    alertDialogBuilder.setMessage("ユーザー名またはパスワードが違います");
                    alertDialogBuilder.setPositiveButton("OK",null);
                    // アラートダイアログを表示します
                    alertDialogBuilder.show();
                }

            }
        });

        screen_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getApplication(), ScreenTwo.class);
                startActivity(intent);
            }
        });

    }
}
SubScreen
package com.example.non.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SubScreen extends AppCompatActivity {

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

    }
}
ScreenTwo
package com.example.non.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class ScreenTwo extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.screen_two_layout);

    }
}
activity_main.xml
<?xml version = "1.0" encoding = "utf-8"?>
<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"
    tools:context = ".MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:text = "Non Login"
        android:layout_width="wrap_content"
        android:layout_height = "wrap_content"
        android:textSize = "30dp"
        android:layout_marginTop="18dp"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/input_user_id"
        android:layout_alignStart="@+id/input_user_id" />

    <EditText
        android:id = "@+id/input_user_id"
        android:layout_width = "200dp"
        android:layout_height = "wrap_content"
        android:focusable = "true"
        android:layout_below="@+id/text_view"
        android:layout_alignLeft="@+id/input_password"
        android:layout_alignStart="@+id/input_password"
        android:layout_marginTop="16dp" />

    <EditText
        android:id="@+id/input_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_marginTop="29dp"
        android:layout_below="@+id/input_user_id"
        android:layout_alignLeft="@+id/login_button"
        android:layout_alignStart="@+id/login_button" />

    <Button
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="11dp"
        android:text="login"
        android:layout_marginLeft="104dp"
        android:layout_marginStart="104dp"
        android:layout_below="@+id/input_password"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/screen_two_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/login_button"
        android:layout_alignEnd="@+id/input_user_id"
        android:layout_alignRight="@+id/input_user_id"
        android:text="Screen 2" />

</RelativeLayout>
sub_screen_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context = ".SubScreen">

    <TextView
        android:id="@+id/success"
        android:textSize="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ログイン成功" />
</RelativeLayout>
screen_two_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context = ".ScreenTwo">
    <TextView
        android:id="@+id/textView"
        android:textSize="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Screen 2" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.non.myapplication"
    android:versionCode="1"
    android:versionName="1.1">

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ScreenTwo"
            android:label="ScreenTwo" >
        </activity>
        <activity
            android:name=".SubScreen"
            android:label="SubScreen" >
        </activity>
    </application>

</manifest>

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5