Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

3.5.UIのセットアップ

hkato edited this page Mar 25, 2016 · 2 revisions

UIの初期化

Java

// アクションボタン
Button btnAction = (Button) findViewById(R.id.btnAction);
if (null != btnAction){
  btnAction.setEnabled(true);
  btnAction.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      v.setEnabled(false);
      if (!_bEstablished) {
        getPeerList();
      } else {
        close();
      }
      v.setEnabled(true);
    }
  });
}

// データ送信ボタン
Button btnSendData = (Button)findViewById(R.id.btnSendData);
if (null != btnSendData){
  btnSendData.setText("Send Data");
  btnSendData.setEnabled(false);
  btnSendData.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
      v.setEnabled(false);
      String data = _editMessage.getText().toString();
      send(data);
      v.setEnabled(true);
    }
  });
}

// 自分のピアID
TextView tvId = (TextView)findViewById(R.id.tvOwnId);
tvId.setTextColor(Color.BLACK);
tvId.setBackgroundColor(Color.LTGRAY);
tvId.setGravity(Gravity.CENTER);

// テキスト入力フィールド
_editMessage = (EditText)findViewById(R.id.editMessage);

// 送受信メッセージのログ
TextView tvLog = (TextView)findViewById(R.id.tvLog);
tvLog.setTextColor(Color.BLACK);
tvLog.setBackgroundColor(Color.LTGRAY);

Objective-C

//IDラベル
    UILabel* lblId = [[UILabel alloc] initWithFrame:rcId];
    [lblId setTag:TAG_ID];
    [lblId setFont:fnt];
    [lblId setTextAlignment:NSTextAlignmentCenter];
    lblId.numberOfLines = 2;
    [lblId setText:@"your ID:\n ---"];
    [lblId setBackgroundColor:[UIColor whiteColor]];
    
    [self.view addSubview:lblId];
    
    
    // アクションボタン
    UIButton* btnCall = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnCall setTag:TAG_WEBRTC_ACTION];
    [btnCall setFrame:rcActionButton];
    [btnCall setTitle:@"Connect to" forState:UIControlStateNormal];
    [btnCall setBackgroundColor:[UIColor lightGrayColor]];
    [btnCall addTarget:self action:@selector(pushCallButton:) forControlEvents:UIControlEventTouchUpInside];
    [btnCall setEnabled:YES];
    
    [self.view addSubview:btnCall];

    
    //テキスト入力フィールド
    _editMessage = [[UITextField alloc]initWithFrame:rcEditMessage];
    [_editMessage setTag:TAG_EDIT_MESSAGE];
    [_editMessage.layer setBorderWidth:0.5f];
    
    [self.view addSubview:_editMessage];
    

    //データ送信ボタン
    UIButton* btnSendData = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnSendData setTag:TAG_SEND_DATA];
    [btnSendData setFrame:rcSendData];
    [btnSendData setTitle:@"Send" forState:UIControlStateNormal];
    [btnSendData setBackgroundColor:[UIColor lightGrayColor]];
    [btnSendData addTarget:self action:@selector(pushSendButton:) forControlEvents:UIControlEventTouchUpInside];
    [btnSendData setEnabled:NO];
    
    [self.view addSubview:btnSendData];
    
    //送信Log
    UITextView* tvLog = [[UITextView alloc] initWithFrame:rcLog];
    [tvLog setTag:TAG_LOG];
    [tvLog setFrame:rcLog];
    [tvLog setBackgroundColor:[UIColor whiteColor]];
    tvLog.layer.borderWidth = 1;
    tvLog.layer.borderColor = [[UIColor orangeColor] CGColor];
    [tvLog setEditable:NO];
    
    [self.view addSubview:tvLog];
略
}

- (void)pushCallButton:(NSObject *)sender
{
    UIButton* btn = (UIButton *)sender;
    
    if (TAG_WEBRTC_ACTION == btn.tag)
    {
        if (nil == _data)
        {
            // Listing all peers
            [self getPeerList];
        }
        else
        {
            // Closing chat
            [self performSelectorInBackground:@selector(close) withObject:nil];
        }
    }
}

- (void)pushSendButton:(NSObject *)sender
{
    UIButton* btn = (UIButton *)sender;
    
    if (TAG_SEND_DATA == btn.tag)
    {
        NSString *data =_editMessage.text;
        [self send:data];
        _editMessage.text = @"";
    }
}

Swift

//設置はstoryboardにて実施
@IBAction func pushCallButton(sender: AnyObject) {
    if _data == nil {
        self.getPeerList()
    }else{
        self.performSelectorInBackground("close", withObject: nil)
    }
}


@IBAction func pushSendButton(sender: AnyObject) {
    let data:String = self.editMessageTextField.text!;
    self.send(data)
    self.editMessageTextField.text = ""
}

UIの更新

Java

private void updateUI() {
  _handler.post(new Runnable() {
    @Override
    public void run() {
      Button btnAction = (Button) findViewById(R.id.btnAction);
      if (null != btnAction) {
        if (false == _bEstablished) {
          btnAction.setText("Connecting");
        } else {
          btnAction.setText("Disconnect");
        }
      }

      TextView tvOwnId = (TextView) findViewById(R.id.tvOwnId);
      if (null != tvOwnId) {
        if (null == _id) {
          tvOwnId.setText("");
        } else {
          tvOwnId.setText(_id);
        }
      }

      Button btnSendData = (Button) findViewById(R.id.btnSendData);
      if (null != btnSendData) {
        btnSendData.setEnabled(_bEstablished);
      }
    }
  });
}

Objective-C

- (void)updateUI
{
    dispatch_async(dispatch_get_main_queue(), ^
       {
           NSString* strTitle = @"---";
           
           UIButton* btn = (UIButton *)[self.view viewWithTag:TAG_WEBRTC_ACTION];
           if (nil != btn)
           {
               if (NO == _bEstablished)
               {
                   strTitle = @"Connect";
               }
               else
               {
                   strTitle = @"Disconnect";
               }
               
               [btn setTitle:strTitle forState:UIControlStateNormal];
           }
           
           //update ID Label
           UILabel* lbl = (UILabel*)[self.view viewWithTag:TAG_ID];
           if (nil == _id)
           {
               [lbl setText:@"your ID: "];
           }
           else
           {
               [lbl setText:[NSString stringWithFormat:@"your ID: \n%@", _id]];
           }
           
           
           //send button
           UIButton* sendbtn = (UIButton *)[self.view viewWithTag:TAG_SEND_DATA];
           if (nil != sendbtn)
           {
               [sendbtn setEnabled:_bEstablished];
           }
       });
}

Swift

func updateUI(){
    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        
        //CALLボタンのアップデート
        if self._bEstablished == false{
            self.callButton.setTitle("CALL", forState: UIControlState.Normal)
        }else{
            self.callButton.setTitle("DISCONNECT", forState: UIControlState.Normal)
        }
        
        //IDラベルのアップデート
        if self.idLabel == nil{
            self.idLabel.text = "your Id:"
        }else{
            self.idLabel.text = "your Id:"+self._id! as String
        }
        
        self.sendButton.enabled = self._bEstablished
    }
}