how to display value of textbox on same screen in android application just like whatsaap

女生的网名这么多〃 提交于 2019-12-02 16:08:16

问题


This is my main file where i am calling the click event on button to pass the value.

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setTitle("aakash");

          SetContentView(R.id.lst);

          Button b = (Button) findViewById(R.id.sendbtn);
          b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(android.view.View v) {
                startActivity(new Intent(View.this,View.class));    
            }
        });   
    }


**xml**
     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#ffffff"
    >

    <TextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:textColor="#000000"
        android:text="aakash"
        android:background="#c0c0c0"
    />

    <ScrollView 
        android:id="@+id/lst"
        android:background="#fefefe"
        android:layout_below="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom_holder"
        >



    </ScrollView>


    <EditText
        android:id="@+id/chattxt"
        android:layout_width="230dp"
        android:layout_height="45dp"
        android:hint="type here"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/sendbtn"
        android:layout_width="75dp"
        android:layout_height="45dp"

        android:text="Send" />

    </LinearLayout>

I want the value of textbox to display on the same screen(scrollview)box and also i want to pass a httppost request to pass a value..plz help me out

Thnks in advance


回答1:


I think, you can use a ListView insted of scroll view. and add list items from the chat text value dynamically. call notifydatasetchanged() every time when you add a new item to it.

<TextView
    android:id="@+id/username"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center_vertical"
    android:layout_alignParentTop="true"
    android:textColor="#000000"
    android:text="aakash"
    android:background="#c0c0c0"
/>

<ListView
                android:id="@+id/listView_chats"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                >
            </ListView>


<EditText
    android:id="@+id/chattxt"
    android:layout_width="230dp"
    android:layout_height="45dp"
    android:hint="type here"
    android:inputType="textMultiLine" />

<Button
    android:id="@+id/sendbtn"
    android:layout_width="75dp"
    android:layout_height="45dp"

    android:text="Send" />

</LinearLayout>

Write a custom adapter for the listView and add the code to manage the list data.




回答2:


i got the answer to my question

import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ButtonActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);


    }
     public void setText(){
            Button myButton = (Button)findViewById(R.id.button1);
            final TextView myText = (TextView)findViewById(R.id.text1);
            final EditText myInput = (EditText)findViewById(R.id.edit);
            myButton.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    myText.setText(myInput.getText()); 
                }
            });    
        }

    }


来源:https://stackoverflow.com/questions/25383169/how-to-display-value-of-textbox-on-same-screen-in-android-application-just-like

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!