Android TableLayout使用時のTextViewの使いまわしについて

このQ&Aのポイント
  • AndroidのTableLayoutを使用してTextViewに値を入れる際、xmlのTextViewを使いまわすことはできるのか?
  • 試した結果、xmlで事前にTextViewを記述しないと使えないことがわかった。
  • TableにTextViewを3つ使いたい場合には、事前にTextViewを3つ記述する必要がある。
回答を見る
  • ベストアンサー

android TableLayout使用時で

いつもお世話になっています。 android TableLayout使用時のことで質問します。 forでTextViewに値を入れていきたいのですが、このときxmlでTextViewをひとつ作っておいてそのTextViewを使いまわすことはできないのでしょうか? したいのが、 for文で、TableRowに値を入れていきたい。 private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private final int FP = ViewGroup.LayoutParams.FILL_PARENT; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TableLayout tableLayout = new TableLayout(this); setContentView(tableLayout); for(int a=0;a<3;a++){ TextView text1 = new TextView(this); text1.setText("user"); TableRow tableRow1 = new TableRow(this); tableRow1.addView(text1); tableLayout.addView(tableRow1, createParam(FP, WC)); } } private TableLayout.LayoutParams createParam(int w, int h){ return new TableLayout.LayoutParams(w, h); } この時表示されたのが、 user user user この方法を、xmlのTextViewを呼ぶ形で実現したいのですが、 xmlでは、 <TableRow> ↓ このTextViewを使いまわしたい。 <TextView android:id="@+id/user" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Test"> </TextView> </TableRow> で、表記しています。 xmlでは、普通に先に記述していないと使えないのでしょうか? つまり、もしTableにTextViewを3つ使いたいなら、TextViewを既に3つ記述していないと使えないものなのでしょうか? よろしくお願いします。

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

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

  • ベストアンサー
noname#161640
noname#161640
回答No.2

ちょっとXMLレイアウトファイルについて、誤解があるような気がします。 XMLによるレイアウトファイルがどのように利用されるのか、うまくイメージ出来ていないのではないでしょうか。XMLレイアウトファイルは、単なるテンプレートに過ぎません。setContentViewでXMLファイル(Rクラスの値)を指定すると、Androidのシステムは、XMLファイルを読み込み、それをもとにコンポーネントのインスタンスをnewし組み込みこんでくれる、ただそれだけのものです。ですから人によっては「XMLレイアウトファイルなんて面倒くさい」と、一切XMLを使わず、全部コードで書く人もいるくらいです。 findViewByIdは、このようにして(既に生成されている)インスタンスを取得し返すものです。別にfindViewByIdしたときにインスタンスが生成されるわけではありません。setContentViewでロードしたときに既にコンポーネント類のインスタンスは生成されていて、単に指定のインスタンスを返しているだけです。ですから、繰り返しでfindViewByIdしても、同じインスタンスが返されるだけで、いくつもインスタンスを作ったりできるわけではありません。 要するに、XMLファイルによるレイアウトの利用は、「new ○○してaddして組み込むという処理をXMLタグを書くだけでやってくれるから、いちいちJavaのコードで書くより便利でしょ?」というだけのものであって、万能ではないのです。ですから、おっしゃるように静的なXMLだけではできないようなことは、コードで書くのが基本です。XMLは単にテンプレートからインスタンスを生成するだけのものだから過剰な期待は禁物です。ダイナミックにインスタンスを操作するなら、コードでごりごり書きましょう。

kannitiha
質問者

お礼

回答ありがとうございます。 >繰り返しでfindViewByIdしても、同じインスタンスが返されるだけで、いくつもインスタンスを作ったりできるわけではありません そうなんですね。 てっきり、できるだけXMLレイアウトで書くのが一番いいと思っていたのですが、そうではないんですね。 >XMLは単にテンプレートからインスタンスを生成するだけのものだから過剰な期待は禁物です。ダイナミックにインスタンスを操作するなら、コードでごりごり書きましょう。 はい。できるだけ、コードで書くことにするようにします。 詳しい説明ありがとうございました。

その他の回答 (1)

noname#161640
noname#161640
回答No.1

よくわからないのですが、ダイナミックにコンポーネントを生成する場合は、XMLは使いません。XMLレイアウトファイルは、事前に静的にコンポーネントを配置するものですので、「書いておいたそのままにコンポーネントが作られる」という形になります。ですから、「もしTableにTextViewを3つ使いたいなら、TextViewを既に3つ記述していないと使えないもの」です。必要に応じてコンポーネントを生成するような場合は、newして組み込んでやります。そういうことですか?

kannitiha
質問者

補足

はい、イメージとしてはそうです。 その場合、newしてxmlを使いまわすことは可能なのでしょうか? 普通に、xmlレイアウトに値を入れる場合、 private TableLayout tableLayout; private TextView tv; private TableRow row; public void onCreate(Bundle icicle){ super.onCreate(icicle); setContentView(R.layout.table); tableLayout = (TableLayout) findViewById(R.id.tableLayout); row = new TableRow(this); tv = (TextView) findViewById(R.id.user); tv.setId(50); tv.setText("chanlenging"); row.addView(tv); tableLayout.addView(row); } ような形でいいのでしょうか? もし、これで、forを使う場合 row = new TableRow(this); for(int a=0;a<3;a++){ tv = (TextView) findViewById(R.id.user); tv.setId(50); tv.setText("chanlenging"); row.addView(tv); } tableLayout.addView(row); のようにして、繰り返すことはできないのでしょうか? やはり、普通にActivitiyクラスに書くのがいいのでしょうか? 宜しくお願いします。

関連するQ&A

  • Android用のアプリケーション開発

    私は下記の開発環境でAndroid用のアプリケーションを開発しています。 Eclipse Version: 3.6.2 Android 2.3.3 TableLayoutで3行3列のテーブルの、4行目の3列目と5行目の3列目を結合したいですが出来ませんでした。 下記のソースコードの「あいうえおかきくけ」の部分を画像にして2行分の大きさで表示させたいです。 Android用のアプリケーションの開発が初めてですが、ご教授を頂けると幸いです。 下記はソースコードの一部です。 --------------------------------------- //1行目 TextView text1 = new TextView(this); text1.setText("         "); TextView text2 = new TextView(this); text2.setText("投資情報"); TextView text3 = new TextView(this); text3.setText("         "); TableRow tableRow1 = new TableRow(this); tableRow1.addView(text1); tableRow1.addView(text2); tableRow1.addView(text3); tableLayout.addView(tableRow1, createParam(FP, WC)); //2行目 TextView text4 = new TextView(this); text4.setText("先週の情報"); TableRow tableRow2 = new TableRow(this); tableRow2.addView(text4); tableLayout.addView(tableRow2, createParam(FP, WC)); //3行目 TextView text8 = new TextView(this); text8.setText("2011年11月7日"); text8.setWidth(480); text8.setGravity(Gravity.RIGHT); TableRow tableRow3 = new TableRow(this); TableRow.LayoutParams rowLayout3 = new TableRow.LayoutParams(); rowLayout3.span = 3; tableRow3.addView(text8, rowLayout3); tableLayout.addView(tableRow3, createParam(FP, WC)); //4行目 ImageView image1 = new ImageView(this); image1.setImageResource(R.drawable.tyuugoku); TextView text11 = new TextView(this); text11.setText("国名"); TextView text12 = new TextView(this); text12.setText("あいうえおかきくけ"); TableRow tableRow4 = new TableRow(this); TableRow.LayoutParams rowLayout4 = new TableRow.LayoutParams(); rowLayout4.column = 2; tableRow4.addView(image1); tableRow4.addView(text11); tableRow4.addView(text12); tableLayout.addView(tableRow4, createParam(FP, WC)); //5行目 TextView text13 = new TextView(this); text13.setText("記事リード"); TableRow tableRow5 = new TableRow(this); TableRow.LayoutParams rowLayout5 = new TableRow.LayoutParams(); tableRow5.addView(text13); tableLayout.addView(tableRow5, createParam(FP, WC)); ---------------------------------------- 長文になりましたが、どうぞ宜しくお願い致します。

    • ベストアンサー
    • Java
  • 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
  • [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でのTextView

    Android javaの質問です。 よろしければ教えてください。 レイアウト構成として、一番上にボタン、中央にテキストビュー、一番下にボタン。 といった感じです。 ボタンの配置はうまくいくのですが、テキストビューがうまく配置されません。 というのも、ここでは省略して書いてませんが、テキストBOXにて文字が入力されたらテキストビューに表示される。 といった仕組みですが、上下のボタンの上に文字が重なってしまいます。 ですので、テキストビュー範囲をを上下ボタンまでとしたいです。 できれば、文字表示も入力されたらLINEのように下から上へと表示されるようにしたいです。 -------省略-------- final int ID_TOP = 1; final int ID_CENTER =2; final int ID_BOTTOM = 3; RelativeLayout layout = new RelativeLayout(this); //上ボタン Button btnTop = new Button(this); btnTop.setId(ID_TOP); btnTop.setText("Top"); RelativeLayout.LayoutParams prmTop = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); prmTop.addRule(RelativeLayout.ALIGN_PARENT_TOP); btnTop.setLayoutParams(prmTop); layout.addView(btnTop); //テキストビュー lblReceive=new TextView(this); lblReceive..setId(ID_CENTER); lblReceive.setText(""); lblReceive.setTextSize(16.0f); lblReceive.setTextColor(Color.rgb(0,0,0)); RelativeLayout.LayoutParams prmCenter = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); prmCenter.addRule(RelativeLayout.CENTER_IN_PARENT); btnCenter.setLayoutParams(prmCenter); layout.addView(lblReceive) //下ボタン Button btnBottom = new Button(this); btnBottom.setId(ID_BOTTOM); btnBottom.setText("Bottom"); RelativeLayout.LayoutParams prmBottom = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); prmBottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); btnBottom.setLayoutParams(prmBottom); layout.addView(btnBottom); -------省略-------- よろしくお願いします。

  • Android開発(Listview)について

    Androidアプリ開発における listviewについて質問させてください。 以下で、東京都,神奈川県,千葉県,埼玉県,茨城県,栃木県,群馬県と表示されています。 東京をクリックしたら、新宿、上野、秋葉原 千葉をクリックしたら、千葉、市川、船橋 といったように、さらに地域を絞っていきたいと考えています。 どのような方法で、実現できるか アドバイスして頂けると助かります。 ■searchActivity.java import android.app.Activity; import android.os.Bundle; import android.widget.*; public class searchActivity extends Activity { private ListView list; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); String[] arr = {"東京都","神奈川県","千葉県","埼玉県","茨城県","栃木県","群馬県"}; // コンポーネントの設定 list = (ListView)this.findViewById(R.id.list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list, arr); list.setAdapter(adapter); } } ■main.xml <?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" android:textSize="20sp" android:background="#ffffff" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> ■list.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="20sp" android:paddingTop="50.0px" android:paddingLeft="20.0px" android:background="#ffffff" android:textColor="#000000" /> 以上、宜しくお願いいたします。

  • 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
  • Android開発(switch文)について

    Android開発(switch文)について Androidアプリ開発における listviewについて質問させてください。 現在、"東京","神奈川","千葉","埼玉","茨城","栃木","群馬" 画面から東京を選び、"新宿","上野","秋葉原"が表示されています。 public void onBackPressed を追加したことにより 一つ前の画面に戻る事ができたのですが このような記述で問題ないでしょうか? もっとシンプルなやり方があれば、アドバイス願います。 また、"新宿","上野","秋葉原"画面から 新宿を選んだ時は、shinjyuku.html 上野を選んだときは、ueno.html 秋葉原を選んだ時は、akihabara.html を表示したいのですが どのタイミングで、switch文を入れれば良いのでしょうか? ■searchActivity.java import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.*; import android.widget.AdapterView.OnItemClickListener; public class searchActivity extends Activity { private ListView list; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); String[] arr = {"東京","神奈川","千葉","埼玉","茨城","栃木","群馬"}; // コンポーネントの設定 list = (ListView)this.findViewById(R.id.list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list, arr); list.setAdapter(adapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { setList2(arg3); } }); } private void setList2(long id) { String[] tokyo = {"新宿","上野","秋葉原"}; String[] kanagawa = {"横浜","桜木町","関内","新横浜"}; String[] chiba = {"千葉","市川","船橋"}; String[] saitama = {"大宮","浦和","蕨","川口・西川口"}; ArrayAdapter<String> adapter = null; switch ((int)id) { case 0: adapter = new ArrayAdapter<String>(this, R.layout.list, tokyo); break; case 1: adapter = new ArrayAdapter<String>(this, R.layout.list, kanagawa); break; case 2: adapter = new ArrayAdapter<String>(this, R.layout.list, chiba); break; case 3: adapter = new ArrayAdapter<String>(this, R.layout.list, saitama); break; } list.setVisibility(View.GONE); ListView list2 = (ListView)this.findViewById(R.id.list2); list2.setAdapter(adapter); list2.setVisibility(View.VISIBLE); } //ここを追加 @Override public void onBackPressed() { //メニュー画面に戻る(start) setContentView(R.layout.main); String[] arr = {"東京","神奈川","千葉","埼玉","茨城","栃木","群馬"}; list = (ListView)this.findViewById(R.id.list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list, arr); list.setAdapter(adapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { setList2(arg3); } }); } } ■main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="20sp" android:background="#ffffff"> <ListView android:id="@+id/list" android:visibility="visible" android:layout_height="fill_parent" android:layout_width="fill_parent"/> <ListView android:id="@+id/list2" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </RelativeLayout> ■list.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="20sp" android:paddingTop="50.0px" android:paddingLeft="20.0px" android:background="#ffffff" android:textColor="#000000" /> 以上、宜しくお願いいたします。

  • androidアプリが強制終了してしまいます。

    オプションメニューボタンの「MENU_ITEM0」を押すと、他の機能が呼び出されずに強制終了でエラーになってしまいます。 ・呼び出すソースファイルです。 「public class CogoterActivity extends Activity { //メニューアイテムID private static final int MENU_ITEM0=0, MENU_ITEM1=1; //アクティビティ起動時に呼ばれる @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); requestWindowFeature(Window.FEATURE_NO_TITLE); //リスト要素の情報群の生成 List<ListItem> items=new ArrayList<ListItem>(); for (int i=0;i<50;i++) { items.add(new ListItem(res2bmp(this,R.drawable.icon),"項目"+i)); } //リストビューの設定 ListView listView=new ListView(this); listView.setScrollingCacheEnabled(false); listView.setAdapter(new ListAdapter(this,items)); setContentView(listView); new TextView(this); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); //メニューアイテム0の追加 MenuItem item0=menu.add(0,MENU_ITEM0,0,"ぶつぶつ"); item0.setIcon(android.R.drawable.ic_menu_edit); //メニューアイテム1の追加 MenuItem item1=menu.add(0,MENU_ITEM1,0,"ぶくぶく"); item1.setIcon(android.R.drawable.ic_menu_more); return true; } //メニューアイテム選択イベントの処理 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_ITEM0: Intent intent = new Intent(this, cogoter.project.MyActivity.class); startActivity(intent); return true; case MENU_ITEM1: showDialog(this,"","ぶくぶくを押した"); return true; } return true; } //ダイアログの表示 private static void showDialog(Context context,String title,String text) { AlertDialog.Builder ad=new AlertDialog.Builder(context); ad.setTitle(title); ad.setMessage(text); ad.setPositiveButton("OK",null); ad.show(); } //リソース→ビットマップ public static Bitmap res2bmp(Context context,int resID) { return BitmapFactory.decodeResource( context.getResources(),resID); } }」 ・呼び出されるソースコード 「public class MyActivity extends Activity implements View.OnClickListener { private final static int WC=LinearLayout.LayoutParams.WRAP_CONTENT; private final static int FP=LinearLayout.LayoutParams.FILL_PARENT; private EditText editText;//エディットテキスト //アクティビティ起動時に呼ばれる @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); requestWindowFeature(Window.FEATURE_NO_TITLE); //戻り値の指定(6) setResult(Activity.RESULT_CANCELED); //インテントからのパラメータ取得(5) String text=""; Bundle extras=getIntent().getExtras(); if (extras!=null) text=extras.getString("text"); //レイアウトの生成 LinearLayout layout=new LinearLayout(this); layout.setBackgroundColor(Color.rgb(255,255,255)); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //OKボタンの生成 Button button=new Button(this); button.setText("OK"); button.setOnClickListener(this); button.setLayoutParams(new LinearLayout.LayoutParams(WC,WC)); layout.addView(button); //エディットテキストの生成 editText=new EditText(this); editText.setText(text); editText.setLayoutParams(new LinearLayout.LayoutParams(FP,WC)); layout.addView(editText); } //ボタンクリック時に呼ばれる public void onClick(View v) { //戻り値の指定(6) Intent intent =new Intent(); intent.putExtra("text",editText.getText().toString()); setResult(Activity.RESULT_OK,intent); //アクティビティの終了(7) finish(); } }」

  • 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アプリ Listviewが

    アドバイスください。初心者です。Listviewを作りたいだけなんです。”データがありません”と表示されてしまいます・・。 ●layout(.xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/android:empty" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="データが存在しません"/> </LinearLayout> ●listview(.java) import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; //ListActivityを継承 import com.example.yamato.myapplication001.R; public class listview extends ListActivity { private String[] items = { "a10", "a11", "a21", "b10" }; //(2) // private String[] items = {};(2) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //リストビューを含むレイアウト指定(1) setContentView(R.layout.activity_main_activity001); // ArrayAdapterオブジェクトの生成 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); // Adapterの指定 setListAdapter(adapter); // ListActivity内部にあるListViewオブジェクトを取得する場合 ListView listView = getListView(); // ListViewオブジェクトを取得したので以下のメソッド等が利用できる // 選択する要素の位置の指定 listView.setSelection(3); } } ソースは参考本からです。

専門家に質問してみよう