【问题标题】:Retrieving and sending a Textview value from/to a Server从/向服务器检索和发送 Textview 值
【发布时间】:2019-02-07 14:21:17
【问题描述】:

我有一个带有按钮的应用程序和一个文本视图(数字例如 100)

我点击按钮然后增加Textview(值)+5。

我正在尝试将值发送到 Php 表行并在同一文本视图中检索表行数据。

在应用程序中关闭并打开,然后根据 Texview 获取 PHP 数据(服务器)

my_table

  • 身份证
  • 用户名
  • 金额

MyActivity.java

public class MainActivity extends AppCompatActivity {
    int minteger = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void increaseInteger(View view) {
        minteger = minteger + 5 ;
        display(minteger);
    }

    private void display(int number) {

        TextView displayInteger = (TextView) findViewById(
                R.id.integer_number);
        displayInteger.setText("Integer: " + number);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_margin="16dp"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click the plus button to increase integer number" />

    <TextView
        android:id="@+id/integer_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:textSize="30sp"
        android:text="Integer: 0" />

    <Button
        android:id="@+id/increase"
        android:onClick="increaseInteger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="INCREASE" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:textSize="20sp"
        android:text="viralandroid.com"/>
</LinearLayout>

【问题讨论】:

  • 如果您想与服务器通信,例如从应用程序发送数据并从服务器检索数据,那么您想在您的 android 项目中设置改造或 volley。Retrofit 和 volley 是外部库用于在 android 中处理 Web 服务

标签: android textview


【解决方案1】:

首先在onCreate 中初始化TextView 和您的Button,然后从display 方法中删除初始化。

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

        displayInteger = (TextView) findViewById(
                    R.id.integer_number);
        Button myButton = (Button) findViewById(
                    R.id.yourButtonId);

        // Here you'll need onClickListener for the button to handle if button clicked, increase the number and etc or calling methods.

}

关注Android Button Onclick 处理Android Button 点击。 但是,关键是,如果您将onClickListener 添加到 Button,您可以调用 display 或只是在onClickListener 中调用setText 而无需其他方法。(简化)

同时添加:

TextView displayInteger;

onCreate 的上方可以访问所有Activity 和当前类。


要将字符串发送到服务器端,请点击此链接:How send data to website by using android app

您需要先通过getText().toString() 获取文本,然后将其作为字符串发送到服务器:How to get EditText value and display it on screen through TextView?

【讨论】:

  • 我认为他需要更多的帮助!像序列化数据、发出请求、解析响应以及多线程......
  • 在我看来这是一个过于宽泛的话题。我已经更新了答案以获取更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 2015-11-12
  • 2016-07-31
相关资源
最近更新 更多