androidでアプリを作ろう

このQ&Aのポイント
  • androidでアプリを作る方法についての質問です。プログラム内で文字列を比較し、結果を表示したいと思っていますが、うまく動作しません。
  • 具体的には、プログラム内でstrInchという変数に格納されたデータと、ユーザーが入力したテキストを比較して、同じ場合は「OK」と表示するようにしたいです。
  • しかし、現在のプログラムはうまく動作していません。どなたか解決方法をご教示いただけないでしょうか?
回答を見る
  • ベストアンサー

androidでアプリを作ろうとしてるのですが

package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class InOutTest extends Activity { /** Called when the activity is first created. */ public TextView txtInfo; public EditText edtText; public Button outputbutton; public TextView txtResult; public TextView txtjudge; public String strInch; public String gettext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //レイアウト作成 ここから LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //レイアウトを見えるようにする //レイアウト作成 ここまで //GUI部品作成 ここから // ラベル txtInfo = new TextView(this); txtInfo.setText("check"); strInch = ("check"); layout.addView(txtInfo); // エディタ edtText = new EditText(this); layout.addView(edtText); // ボタン txtResult = new Button(this); txtResult.setText("same or difference"); layout.addView(txtResult); // 結果表示用ラベル txtjudge = new TextView(this); txtjudge.setText("check"); layout.addView(txtjudge); //GUI部品作成 ここまで txtResult.setOnClickListener(new OnClickListener(){ public void onClick(View v) { gettext = edtText.toString(); if (strInch==gettext){ txtjudge.setText("ok"); } }; }); }} 上のプログラムで strInchに入ってるデータとgettextで取得した文章を比較して同じなら txtjudgeにOKと引き渡して表示させたいのですがうまく動きません 何方かどうぞよろしくお願い致します

  • Java
  • 回答数2
  • ありがとう数3

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

> if (strInch==gettext){ if (strInch.equals(gettext)) { じゃないとダメだと思う。

4394en
質問者

お礼

ありがとうございました 解決できました

その他の回答 (1)

noname#161640
noname#161640
回答No.2

gettext = edtText.toString(); これは……toStringで入力テキストが得られます? gettext = (String)edtText.getText(); あるいは、 Editable gettext = editText.getText(); といった感じでしょう。

4394en
質問者

お礼

ありがとうございます 下の情報と合わせて実行することで解決できました

関連するQ&A

  • androidプログラミングについての質問

    このプログラムについて質問したいのですが public class InOutTest extends Activity { /** Called when the activity is first created. */ public TextView txtInfo; public EditText edtText; public Button outputbutton; public TextView txtResult; public TextView txtjudge; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //レイアウト作成 ここから LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //レイアウトを見えるようにする //レイアウト作成 ここまで //GUI部品作成 ここから // ラベル txtInfo = new TextView(this); txtInfo.setText("check spell same or not:"); layout.addView(txtInfo); // エディタ edtText = new EditText(this); layout.addView(edtText); // ボタン txtResult = new Button(this); txtResult.setText("same or difference"); layout.addView(txtResult); // 結果表示用ラベル txtjudge = new TextView(this); txtjudge.setTextSize(30f); layout.addView(txtjudge); //GUI部品作成 ここまで txtResult.setOnClickListener(new OnClickListener(){ public void onClick(View v) { String strInch = ""; if (edtText==txtInfo) strInch = "typed as same"; txtResult.setText(strInch); } }); } } txtResultに文章を引き渡して表示させたいのですが やり方が分かりません お時間のある方でいいので どなたかよろしければ教えてください

  • Androidでこのエラーを解決したいです

    どなたかご教授お願いします package com.example.kusogame; import宣言省略 public class TypingKusoGame extends Activity { /** Called when the activity is first created. */ public TextView txtInfo; public EditText edtText; public Button outputbutton; public Button createtext; public TextView txtResult; public TextView Title; public TextView txtjudge; public String strInch; public String gettext; public String test = "check"; public ImageView judgecat; public int idx = 0; private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; public int createParam; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //レイアウト作成 ここから LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //レイアウトを見えるようにする //レイアウト作成 ここまで //GUI部品作成 ここから //タイトル Title = new TextView(this); Title.setText("Let's Training!"); Title.setTypeface(Typeface.DEFAULT_BOLD); Title.setTextScaleX(1.5f); Title.setTextColor(Color.rgb(255,0,0)); layout.addView(Title); // ラベル txtInfo = new TextView(this); txtInfo.setText("check"); txtInfo.setTextScaleX(1.5f); strInch = ("check"); layout.addView(txtInfo); // エディタ edtText = new EditText(this); layout.addView(edtText); // ボタン txtResult = new Button(this); txtResult.setText("same or difference"); layout.addView(txtResult); // 結果表示用ラベル txtjudge = new TextView(this); txtjudge.setText("check"); layout.addView(txtjudge); //結果表示用猫ラベル judgecat = new ImageView(this); judgecat.setImageResource(R.drawable.failed); LinearLayout.addView(judgecat,LayoutParams(WC)); ~~~~~~~~~~~~~~このエラーが解決できません メソッドLayoutParam(int)は 型TypingKusoGameで未定義です //GUI部品作成 ここまで txtResult.setOnClickListener(new OnClickListener(){ pub

    • ベストアンサー
    • Java
  • このプログラムについて質問したいのですが

    public class InOutTest extends Activity { /** Called when the activity is first created. */ public TextView txtInfo; public EditText edtText; public Button outputbutton; public TextView txtResult; public TextView txtjudge; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //レイアウト作成 ここから LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //レイアウトを見えるようにする //レイアウト作成 ここまで //GUI部品作成 ここから // ラベル txtInfo = new TextView(this); txtInfo.setText("check spell same or not:"); layout.addView(txtInfo); // エディタ edtText = new EditText(this); layout.addView(edtText); // ボタン txtResult = new Button(this); txtResult.setText("same or difference"); layout.addView(txtResult); // 結果表示用ラベル txtjudge = new TextView(this); txtjudge.setTextSize(30f); layout.addView(txtjudge); //GUI部品作成 ここまで txtResult.setOnClickListener(new OnClickListener(){ public void onClick(View v) { String strInch = ""; if (edtText==txtInfo) strInch = "typed as same"; txtResult.setText(strInch); } }); } } txtResultに文章を引き渡して表示させたいのですが やり方が分かりません お時間のある方でいいので どなたかよろしければ教えてください

    • ベストアンサー
    • Java
  • 作成中のandroidアプリにタイマを組み込みたい

    カウントダウンタイマーを現在作成中のアプリに組み込みたいのですが 調べてもイマイチやり方が分からないでいます 秒数は固定でもいいのでなるべく単純なものをお願いします 作成中のプログラムは以下に記述しておきます 使えるページと組み込みの手順を何方かご教授くださいませ もし私のプログラムを使いたいという方がいらっしゃいましたらどうぞご自由に 文字制限の関係で一部省略 package com.example.kusogame; import *省略 public class TypingKusoGame extends Activity { /** Called when the activity is first created. */ public 宣言省略 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //レイアウト作成 ここから LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //レイアウトを見えるようにする //レイアウト作成 ここまで //GUI部品作成 ここから //タイトル Title = new TextView(this); Title.setText("Let's Training!"); layout.addView(Title); // ラベル txtInfo = new TextView(this); txtInfo.setText("check"); strInch = ("check"); layout.addView(txtInfo); // エディタ edtText = new EditText(this); layout.addView(edtText); // ボタン txtResult = new Button(this); txtResult.setText("same or difference"); layout.addView(txtResult); // 結果表示用ラベル txtjudge = new TextView(this); txtjudge.setText("check"); layout.addView(txtjudge); //GUI部品作成 ここまで txtResult.setOnClickListener(new OnClickListener(){ public void onClick(View v) { gettext = edtText.getText().toString(); if (gettext.equals(test)){ txtjudge.setText("ok"); switch(idx){// サンプル文の生成 case 0: txtInfo.setText("copyit"); test ="copyit"; break; case 1: txtInfo.setText("androidboy"); test ="check"; break; case 2: txtInfo.setText("laugh"); test ="laugh";break; case 3: txtInfo.setText("training"); test ="training"; break; default : //添字のリセット txtInfo.setText("reset"); test ="reset"; idx = 0; break; } idx += 1; //添字を1増やす }else{txtjudge.setText("no!");} }; }); } }

    • ベストアンサー
    • Java
  • Android開発 Eclipse(超初心者)

    Eclipseの使い方がわかりません。 eclipseをインストールしてプロジェクトを作成しAVDの仮想デバイスを適当に作成し AVD仮想デバイス ターゲット:Android4.0-APILevel14 SDカード:サイズ 66MiB スキン:ビルトイン WVGA800 package com.example.android.hello.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class Hello15 extends Activity{ //表示用のテキストとボタンを宣言 --- private TextView txtCaption; private Button btnHello; public void OnCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //レイアウトを作成 LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //テキストを追加 txtCaption = new TextView(this); txtCaption.setText("Click Button!"); layout.addView(txtCaption); //ボタンを追加 btnHello = new Button(this); btnHello.setText("Button"); layout.addView(btnHello); //イベントを設定 btnHello.setOnClickListener(new OnClickListener(){ public void onClick(View v){ txtCaption.setText("Hello, Android!"); } }); } } サンプルで上記のようなソースを作成して 実行したのですが 以前は Android Application Java Applet Java Application など選択肢が出てたのですが仮想デバイスを設定した 辺りからでなくなり実行して少し経つと ←画面に『android』という文字が →画面にキーボードのようなものがでてきて 3分ぐらいまつと←画面が携帯画面に変化します 上記のプログラムではボタンの生成とメッセージがでてくるはずが それらしきものがありません。これらはどこに出てくるのでしょうか? 動かし方すらよくわかってないのでお解説おねがいいたしもうす;

  • [Android]エミュレータで動作しない原因

    こんにちは。java、Android開発初心者です。 「test001」というAndroidプロジェクト、「test001_01」というクラスを作成し、マニフェストを書き換えました。実行すると、再起動して繰り返しても The application has stopped unexpectedly. Please try again. と表示されます。 原因と対処方法を教えていただけませんか。 宜しくお願い致します。 ★test001 Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test001" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.test001.text001_01" 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> ★test001_01.java package com.example.test001; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.Menu; import android.widget.Button; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class test001_01 extends Activity { private Button startButton, stopButton; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sub); startButton = new Button(this); startButton.setText("Button5"); stopButton = new Button(this); stopButton.setText("Button6"); textView = new TextView(this); textView.setText("TextView1"); textView.setBackgroundColor(Color.YELLOW); LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setLayoutParams(linearLayoutParams); linearLayout.addView(startButton, linearLayoutParams); linearLayout.addView(stopButton, linearLayoutParams); linearLayout.addView(textView, linearLayoutParams); setContentView(linearLayout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }

    • ベストアンサー
    • Java
  • Android JAVAへでの質問です

    (←※この部分!) の意味がわからないのでどなたか教えていただけないでしょうか? private EditText edit1; ですが、なぜ private がついているのでしょうか?なくても動きましたが何か意味があるのでしょうか? そしてどうして、この子はここにいるのでしょうか? Button btn1 と一緒に仲良くいてもいいんじゃないでしょうか? ================ import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class Test2Activity extends Activity { private EditText edit1;(←※この部分!) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edit1 = (EditText)this.findViewById(R.id.EditText1); Button btn1 = (Button)this.findViewById(R.id.button1); btn1.setOnClickListener(new MyClickA()); } class MyClickA implements OnClickListener { public void onClick(View view){ Editable e = edit1.getText(); int n = Integer.parseInt(e.toString()); Toast toast = Toast.makeText(getApplicationContext(), "数値=" + n, Toast.LENGTH_SHORT); toast.show(); } } }

    • ベストアンサー
    • Java
  • androidアプリ開発について

    androidアプリ開発初心者です。 やりたいことは、 スタートボタンを押して、自分のお気に入りのページに飛び、(例えばオークションなど)サイトを表示させ、何分間ごとに更新できるループ処理?などができればいいなと考えています。 スタートボタンを押してサイトを表示するまでは何とか作れたのですが、それ以降をどのように作ったらいいのか分かりません。 初心者的な質問で申し訳ないのですが、どのように書いたらよいのか教えてください。 コードはこのようになっています。 <<main.xml>> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="192dp" android:layout_height="wrap_content" android:text="スタート" /> </LinearLayout> <<MainActivity.java>> import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Uri uri =Uri.parse("http://"); Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 長文で申し訳ありません。どうぞ宜しくお願い致します。

  • androidのアプリを作ってるんですが

    import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView; import android.content.Intent; import android.widget.ImageView; public class GameActivity extends Activity implements OnTouchListener{ private ImageView target; @Override protected void onCreate(Bundle savedInstanceState) { target = (ImageView)findViewById(R.id.imageView1); super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); target.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent motion) { switch(motion.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: break; } return true; } } これからゲーム本体を作っていこうとしてるんですが、onTouchメソッドを実装してから実機で動作しなくなり、先に進めず困っています。なぜこれで動作しないのか私の知識不足でよくわかりません。 回答お願いいたします。

    • ベストアンサー
    • Java
  • android HashMapについて助言お願いし

    初心者ですが、どうぞ宜しくお願いします。 下記コードでは、マップに 「 map.put( "Key1", "val1" ); map.put( "key2", "val2" ); 」を追加しているのですが、このデータをキーから取り出しテキストビューに表示しようとしています。 このままではエラーはないのですが、何も表示されない状況です。 初心者の無知な質問となりますが、 テキストビューへの表示方法はどのようにすればよいのでしょうか? 何か別の方法でもよいので、ご教授宜しくお願いいたします。 -------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1"></TextView> </LinearLayout> ------------------------------------------------------------ package com.test; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //1)HashMapからMapのインスタンスを生成 HashMap<String, String> map = new HashMap<String, String>(); // キーと値のペアを格納 map.put( "Key1", "val1" ); map.put( "key2", "val2" ); // 指定したキーに対応する値を取得. String val = (String)map.get( "key1" ); // テキストビュー取得 TextView tv = (TextView)findViewById(R.id.tv1); tv.setText(val); } }

    • ベストアンサー
    • Java