0

レイアウトファイル-listitem_discuss.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="wrap_content" >


<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">"

    <TextView
        android:id="@+id/comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="Hello bubbles!"
        android:textColor="@android:color/primary_text_light" />

    <TextView 
        android:id="@+id/tstextbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginBottom="5dip"
        android:text="TimeStamp"/>
</LinearLayout>

</LinearLayout>

レイアウトファイル-activity_discuss.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:orientation="vertical" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/bg" 
    android:scaleType="matrix"
    />


<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="115dp"
    android:layout_marginBottom="10dp"
    android:layout_above="@+id/form"
    android:layout_alignParentTop="true" >
</ListView>

<RelativeLayout
    android:id="@+id/form"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:ems="10"
        android:hint="Talk to me"
        android:inputType="text" />
</RelativeLayout>

</RelativeLayout>

Java クラス ファイル * Page1.java *

public class Page1 extends Activity {
private com.anr.aifv1.DiscussArrayAdapter adapter;
private ListView lv;
private EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Remove notification bar
//this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discuss);
    lv = (ListView) findViewById(R.id.listView1);
adapter = new DiscussArrayAdapter(getApplicationContext(),R.layout.listitem_discuss);
lv.setAdapter(adapter);
editText1 = (EditText) findViewById(R.id.editText1);
editText1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
adapter.add(new OneComment(false, editText1.getText().toString()));

adapter.add(new OneComment(true, "Thank you"));
    editText1.setText("");
//tstextboxjv.setText("Sample");
return true;
         }
return false;
        }
    });
}
}

ディスカッションArrayAdapter.java

package com.anr.aifv1;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
    import android.widget.TextView;

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {


private TextView countryName;
private List<OneComment> countries = new ArrayList<OneComment>();
private LinearLayout wrapper;

@Override
public void add(OneComment object) {
    countries.add(object);
    super.add(object);
}

public DiscussArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}

public int getCount() {
    return this.countries.size();
}

public OneComment getItem(int index) {
    return this.countries.get(index);
}

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.listitem_discuss, parent, false);
    }

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

    OneComment coment = getItem(position);

    countryName = (TextView) row.findViewById(R.id.comment);


    countryName.setText(coment.comment);

    countryName.setBackgroundResource(coment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
    wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

    return row;
}

public Bitmap decodeToBitmap(byte[] decodedByte) {
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

}

OneComment.java

package com.anr.aifv1;

import java.util.Calendar;

import android.widget.TextView;

public class OneComment {
public boolean left;
public String comment;

public OneComment(boolean left, String comment) {
    super();

    this.left = left;
    this.comment = comment;
}



}

これはうまくいきます。しかし、コメント文 "tstextboxjv.setText("Sample");" アプリのコメントを外します。クラッシュします。

私の目的は、TextView を使用してチャット バブルのタイムスタンプを表示することです。

Logcat スタックトレース

04-14 13:11:48.094: D/AndroidRuntime(940): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
04-14 13:11:48.094: D/AndroidRuntime(940): CheckJNI is ON
04-14 13:11:48.144: D/dalvikvm(940): Trying to load lib libjavacore.so 0x0
04-14 13:11:48.154: D/dalvikvm(940): Added shared lib libjavacore.so 0x0
04-14 13:11:48.194: D/dalvikvm(940): Trying to load lib libnativehelper.so 0x0
04-14 13:11:48.204: D/dalvikvm(940): Added shared lib libnativehelper.so 0x0
04-14 13:11:48.934: D/AndroidRuntime(940): Calling main entry com.android.commands.pm.Pm
04-14 13:11:48.994: D/AndroidRuntime(940): Shutting down VM
04-14 13:11:49.025: D/dalvikvm(940): GC_CONCURRENT freed 102K, 78% free 466K/2048K, paused 6ms+9ms, total 32ms
04-14 13:11:49.025: D/jdwp(940): Got wake-up signal, bailing out of select
04-14 13:11:49.025: D/dalvikvm(940): Debugger has detached; object registry had 1 entries
04-14 13:11:49.664: D/AndroidRuntime(954): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
04-14 13:11:49.664: D/AndroidRuntime(954): CheckJNI is ON
04-14 13:11:49.744: D/dalvikvm(954): Trying to load lib libjavacore.so 0x0
04-14 13:11:49.744: D/dalvikvm(954): Added shared lib libjavacore.so 0x0
04-14 13:11:49.785: D/dalvikvm(954): Trying to load lib libnativehelper.so 0x0
04-14 13:11:49.794: D/dalvikvm(954): Added shared lib libnativehelper.so 0x0
04-14 13:11:50.554: D/AndroidRuntime(954): Calling main entry com.android.commands.am.Am
04-14 13:11:50.604: I/ActivityManager(174): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.anr.aifv1/.Page1 u=0} from pid 954
04-14 13:11:50.634: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.634: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.634: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.634: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.634: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.634: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.644: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.644: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.644: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.644: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:11:50.644: W/WindowManager(174): Failure taking screenshot for (266x425) to layer 21005
04-14 13:11:50.665: D/AndroidRuntime(954): Shutting down VM
04-14 13:11:50.734: I/AndroidRuntime(954): NOTE: attach of thread 'Binder_4' failed
04-14 13:11:50.744: D/dalvikvm(966): Not late-enabling CheckJNI (already on)
04-14 13:11:50.764: I/ActivityManager(174): Start proc com.anr.aifv1 for activity com.anr.aifv1/.Page1: pid=966 uid=10043 gids={1028}
04-14 13:11:50.774: D/dalvikvm(954): GC_CONCURRENT freed 102K, 77% free 489K/2048K, paused 30ms+1ms, total 99ms
04-14 13:11:50.774: D/jdwp(954): Got wake-up signal, bailing out of select
04-14 13:11:50.784: D/dalvikvm(954): Debugger has detached; object registry had 1 entries
04-14 13:11:51.294: E/Trace(966): error opening trace file: No such file or directory (2)
04-14 13:11:52.154: D/dalvikvm(966): GC_FOR_ALLOC freed 60K, 5% free 7252K/7555K, paused 52ms, total 54ms
04-14 13:11:52.174: I/dalvikvm-heap(966): Grow heap (frag case) to 9.518MB for 2457616-byte allocation
04-14 13:11:52.424: D/dalvikvm(966): GC_CONCURRENT freed <1K, 4% free 9652K/9991K, paused 74ms+13ms, total 247ms
04-14 13:11:52.618: D/dalvikvm(966): GC_FOR_ALLOC freed 0K, 4% free 9652K/9991K, paused 35ms, total 35ms
04-14 13:11:52.684: I/dalvikvm-heap(966): Grow heap (frag case) to 13.672MB for 4356164-byte allocation
04-14 13:11:52.897: D/dalvikvm(966): GC_CONCURRENT freed 0K, 3% free 13906K/14279K, paused 74ms+16ms, total 216ms
04-14 13:11:53.385: D/gralloc_goldfish(966): Emulator without GPU emulation detected.
04-14 13:11:53.526: I/ActivityManager(174): Displayed com.anr.aifv1/.Page1: +2s816ms
04-14 13:11:54.454: I/Choreographer(333): Skipped 64 frames!  The application may be doing too much work on its main thread.
04-14 13:12:06.664: E/PowerManagerService(174): Excessive delay setting brightness: 126ms, mask=2
04-14 13:12:07.084: E/PowerManagerService(174): Excessive delay setting brightness: 140ms, mask=2
04-14 13:12:10.284: D/AndroidRuntime(966): Shutting down VM
04-14 13:12:10.334: W/dalvikvm(966): threadid=1: thread exiting with uncaught exception (group=0x2bc9a300)
04-14 13:12:10.508: E/AndroidRuntime(966): FATAL EXCEPTION: main
04-14 13:12:10.508: E/AndroidRuntime(966): java.lang.NullPointerException
04-14 13:12:10.508: E/AndroidRuntime(966):  at com.anr.aifv1.Page1$1.onKey(Page1.java:53)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.View.dispatchKeyEvent(View.java:7081)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
04-14 13:12:10.508: E/AndroidRuntime(966):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1892)
04-14 13:12:10.508: E/AndroidRuntime(966):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1369)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.app.Activity.dispatchKeyEvent(Activity.java:2356)
04-14 13:12:10.508: E/AndroidRuntime(966):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1819)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3577)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3533)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3115)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2932)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.os.Looper.loop(Looper.java:137)
04-14 13:12:10.508: E/AndroidRuntime(966):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-14 13:12:10.508: E/AndroidRuntime(966):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 13:12:10.508: E/AndroidRuntime(966):  at java.lang.reflect.Method.invoke(Method.java:511)
04-14 13:12:10.508: E/AndroidRuntime(966):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-14 13:12:10.508: E/AndroidRuntime(966):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-14 13:12:10.508: E/AndroidRuntime(966):  at dalvik.system.NativeStart.main(Native Method)
04-14 13:12:10.606: W/ActivityManager(174):   Force finishing activity com.anr.aifv1/.Page1
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: E/libEGL(35): called unimplemented OpenGL ES API
04-14 13:12:10.614: W/WindowManager(174): Failure taking screenshot for (266x425) to layer 21015
04-14 13:12:10.634: I/Process(966): Sending signal. PID: 966 SIG: 9
04-14 13:12:10.714: W/InputDispatcher(174): channel '2c56a158 com.anr.aifv1/com.anr.aifv1.Page1 (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
04-14 13:12:10.714: E/InputDispatcher(174): channel '2c56a158 com.anr.aifv1/com.anr.aifv1.Page1 (server)' ~ Channel is unrecoverably broken and will be disposed!
04-14 13:12:10.745: I/WindowState(174): WIN DEATH: Window{2c56a158 com.anr.aifv1/com.anr.aifv1.Page1 paused=true}
04-14 13:12:10.745: I/ActivityManager(174): Process com.anr.aifv1 (pid 966) has died.
04-14 13:12:10.754: W/InputDispatcher(174): Attempted to unregister already unregistered input channel '2c56a158 com.anr.aifv1/com.anr.aifv1.Page1 (server)'
04-14 13:12:10.784: I/WindowManager(174): WINDOW DIED Window{2c56a158 com.anr.aifv1/com.anr.aifv1.Page1 paused=true}
04-14 13:12:10.914: I/Choreographer(333): Skipped 54 frames!  The application may be doing too much work on its main thread.
04-14 13:12:11.037: W/InputMethodManagerService(174): Got RemoteException sending setActive(false) notification to pid 966 uid 10043
04-14 13:12:11.454: I/Choreographer(333): Skipped 50 frames!  The application may be doing too much work on its main thread.
04-14 13:12:11.464: D/dalvikvm(174): GC_CONCURRENT freed 322K, 20% free 9459K/11783K, paused 75ms+39ms, total 774ms
04-14 13:12:12.124: I/Choreographer(333): Skipped 49 frames!  The application may be doing too much work on its main thread.

編集

 public class Page1 extends Activity {
private com.anr.aifv1.DiscussArrayAdapter adapter;
private ListView lv;
private EditText editText1;
private TextView tstextboxjv;
@Override
protected void onCreate(Bundle savedInstanceState) {

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Remove notification bar
//this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discuss);
    lv = (ListView) findViewById(R.id.listView1);
adapter = new DiscussArrayAdapter(getApplicationContext(),R.layout.listitem_discuss);
lv.setAdapter(adapter);
editText1 = (EditText) findViewById(R.id.editText1);
tstextboxjv = (TextView) findViewById(R.id.tstextbox);
editText1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
adapter.add(new OneComment(false, editText1.getText().toString()));

adapter.add(new OneComment(true, "Thank you"));
    editText1.setText("");
tstextboxjv.setText("Sample");
return true;
         }
return false;
        }
    });
}
  }
4

2 に答える 2

3

テキストビューのIDを見つけて、テキストビューにテキストを設定する必要があります。

string、drawables などのすべてのリソース、textview などのビューは R.java にエントリがあります。そのため、ID を検索する必要があります。int 値である R.id.tstextbox 。xml レイアウトに textview を追加し、エントリを保存すると、R.java に自動的に作成されます。R.java は自動生成されます。

あなたの R.java には、次のように textview のエントリがあります。

      public static final int tstextbox=0x7f070001;
      // the value may be different
      // will look something similar to above.

次に、textview id を見つけて、アクティビティ クラスの textview にテキストを設定できます。

    <TextView 
    android:id="@+id/tstextbox" // id of textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:layout_marginBottom="5dip"
    android:text="TimeStamp"/>

あなたの活動で onCreate()

      TextView tv=(TextView) findViewById(R.id.tstextbox);
      tv.setText("sample");  

編集:

以下を試してみましたが、デバイスで正常に動作します

    et.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    // If the event is a key-down event on the "enter" button
    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
    // Perform action on key press
     et.setText("");
    _tv.setText("Sample");
    return true;
             }
    return false;
            }

また、リストビューに新しいデータを追加する場合は、アダプターで notifyDataSetChanged() を呼び出してリストビューを更新する必要があります。

編集2:

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >

 <TextView
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Blank text" />


 <EditText
    android:id="@+id/ed"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_marginTop="40dp" />
 </LinearLayout>

次に、私の MainActivity で

 setContentView(R.layout.activity_main);
 _tv = (TextView) findViewById( R.id.tv );
 et = (EditText) findViewById(R.id.ed);
 et.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    // If the event is a key-down event on the "enter" button
    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
    // Perform action on key press

     et.setText("");
    _tv.setText("Sample");
    return true;
             }
    return false;
            }

TextView と EditText は同じレイアウト xml ファイル、つまり activity_discuss にある必要があります。

次に、同じIDを取得して上記を実行できます。

別のレイアウトでテキストビューのテキストを設定する必要がある場合は、以下のリンクを確認してください

EditText と Text View の間でデータを転送する

于 2013-04-14T07:27:24.757 に答える
0

1. javaファイルにTextViewのレイアウトファイルを定義する

setContentView(R.layout.activity);

2. TextView にリンクする変数を定義する

private TextView variable_name;

3. 変数をビューにリンクする

variable_name = (TextView) findViewById (R.id.TextView_name);

4. テキストを設定する

variable_name.setText("Sample_Text");
于 2013-04-15T05:53:01.067 に答える