Newer
Older
RxSprout / app / src / main / java / com / example / sprout / rooms / RoomCreateActivity.java
package com.example.sprout.rooms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.example.sprout.R;
import com.example.sprout.R.id;
import com.example.sprout.R.layout;
import com.example.sprout.Sprout;

import net.arnx.jsonic.JSON;

import framework.network.CallBack;

public class RoomCreateActivity extends Activity {
    private EditText roomName;
    private EditText Key;
    private RoomsConnection connection;
    private Sprout sprout;
    private boolean key_check = true;

    //最初に処理
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_room_create);
        sprout = (Sprout) getApplication();
        roomName = (EditText) findViewById(id.roomcreate_editText1);
        Key = (EditText) findViewById(id.roomcreate_editText2);
        //ラジオボタン
        radio_button();
        //戻るボタン
        back_button();
        //作成ボタン
        room_create_button();
    }

    //ラジオボタン
    public void radio_button() {
        RadioGroup radioGroup = (RadioGroup) findViewById(id.roomcreate_radiogroup);
        radioGroup.check(id.roomcreate_radioButton1);
        RadioButton radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                //なし

                if (checkedId == id.roomcreate_radioButton2) {
                    key_check = false;
                    Key.setVisibility(View.INVISIBLE);
                    Key.getEditableText().clear();
                }
                //あり
                else {
                    key_check = true;
                    Key.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //戻るボタン
    public void back_button() {
        Button backButton = (Button) findViewById(id.roomcreate_button1);
        backButton.setOnClickListener(new View.OnClickListener() {
            //押したらルーム一覧画面へ戻る
            public void onClick(View v) {
                Intent intent = new Intent(RoomCreateActivity.this, RoomListActivity.class);
                startActivity(intent);
            }
        });
    }

    //作成ボタン
    public void room_create_button() {
        Button createButton = (Button) findViewById(id.roomcreate_button2);
        createButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                connection = sprout.getRoomsConnection();
                connection.setCallBack(new CallBack() {
                    @Override
                    //レスポンスが来た時に解読し実行
                    public void onResponse(String response) {
                        RoomResponse room = JSON.decode(response, RoomResponse.class);
                        ((Sprout) RoomCreateActivity.this.getApplication()).setRoomId(room.getRoom().getRoomId());
                        ((Sprout) RoomCreateActivity.this.getApplication()).setMemberId(room.getMemberId());
                        //押したらルーム画面へ進む
                        Intent intent = new Intent(RoomCreateActivity.this, RoomMainActivity.class);
                        startActivity(intent);
                    }
                });

                //パスワードありかつパスワード未入力の場合
                if (key_check == true && Key.length() == 0 || roomName.length() == 0) {
                    Toast.makeText(getBaseContext(), "ルーム名かパスワードが未入力です!", Toast.LENGTH_SHORT).show();
                } else {
                    //サーバに送る
                    int userId = ((Sprout) getApplication()).getUserId();
                    connection.addFormParam("userId", String.valueOf(userId));
                    connection.addFormParam("roomName", roomName.getText().toString());
                    if (Key.length() != 0) {
                        connection.addFormParam("key", Key.getText().toString());
                    }
                    connection.doPost();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.room_create, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}