実現したい機能
1.スマートフォンAとスマートフォンBをソケット通信で通信する機能
2.スマートフォンAがボタンを押して、スマートフォンBにメッセージを送信する。(このとき、ボタン押下と同時にスマートフォンAのストップウォッチが動作開始)
3.スマートフォンBにメッセージが届いたら、画面のどこでも良いからタップする(ボタンは作りません!)とスマートフォンBのストップウォッチが作動する。(このとき、スマートフォンAにイベントを送信して、スマートフォンAのストップウォッチを止める)
といった、要はストップウォッチの遠隔操作機能を作りたいです。
ちなみに、1、2の機能は正しく動作できることを確認しました。
3に関して、画面のどこかをタップすると、スマートフォンBのストップウォッチが作動する機能と、スマートフォンAにイベントを送信して、スマートフォンAのストップウォッチを止める機能ができていません。
今は、画面のどこかをタップすると、スマートフォンBのストップウォッチが作動する機能のみを作成中です。(ストップウォッチの初期タイプを作るためにあえてボタンを作っています。ボタンではなく、タップしたらストップウォッチが動作するようにするにはどうプログラムをすればよいでしょうか?)
エラーメッセージ
Error:(32, 33) エラー: 不適合な型: ViewをTextViewに変換できません:
Error:(35, 35) エラー: 不適合な型: ViewをButtonに変換できません:
Error:(38, 41) エラー: 不適合な型: ViewをButtonに変換できません:
どうも、findViewByIdがダメなようなのですが、、
###ソースコード
以下にソースコードを掲載しますが、参考サイトなど教えていただけると幸いです。
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Locale; import java.text.SimpleDateFormat; import android.os.Handler; public class MainActivity extends AppCompatActivity implements Runnable, View.OnClickListener { private long startTime; private TextView timerText; private Button startButton; private final Handler handler = new Handler(); private volatile boolean stopRun = false; private SimpleDateFormat dataFormat = new SimpleDateFormat("mm:ss.SS", Locale.JAPAN); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timerText = findViewById(R.id.timer); timerText.setText(dataFormat.format(0)); startButton = findViewById(R.id.start_button); startButton.setOnClickListener(this); Button stopButton = findViewById(R.id.stop_button); stopButton.setOnClickListener(this); // タッチイベント(画面タッチを感知する) } @Override public boolean onTouchEvent(MotionEvent event) { // 文字の変更 ((TextView) findViewById(R.id.text)).setText("タッチしたよ!!"); return super.onTouchEvent(event); } @Override public void onClick(View v) { Thread thread; if (v == startButton) { stopRun = false; thread = new Thread(this); thread.start(); startTime = System.currentTimeMillis(); } else { stopRun = true; timerText.setText(dataFormat.format(0)); } } @Override public void run() { // 10 msec order int period = 10; while (!stopRun) { // sleep: period msec try { Thread.sleep(period); } catch (InterruptedException e) { e.printStackTrace(); stopRun = true; } handler.post(new Runnable() { @Override public void run() { long endTime = System.currentTimeMillis(); // カウント時間 = 経過時間 - 開始時間 long diffTime = (endTime - startTime); timerText.setText(dataFormat.format(diffTime)); } }); } } }
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <TextView 8 android:textSize="40sp" 9 android:id="@+id/text" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="タッチしてね!" 13 android:layout_alignParentTop="true" 14 android:layout_centerHorizontal="true"/> 15 "/> 16 17 <Button 18 android:text="start" 19 android:id="@+id/start_button" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:layout_marginLeft="10dp" 23 android:layout_marginRight="10dp" 24 android:layout_centerVertical="true" 25 android:layout_alignParentEnd="true"/> 26 27 <Button 28 android:text="stop" 29 android:id="@+id/stop_button" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:layout_marginLeft="10dp" 33 android:layout_marginRight="10dp" 34 android:layout_marginBottom="133dp" 35 android:layout_alignParentBottom="true" 36 android:layout_alignStart="@+id/start_button"/> 37 38 <TextView 39 android:id="@+id/timer" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="TextView" 43 /> 44</RelativeLayout>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。