繁体   English   中英

显示从edittext到textview的文本

[英]Displaying text from from edittext to textview

我有一个天气应用程序,它要求您从编辑文本字段中获取文本并将其显示在文本视图中,我正在努力做到这一点,所以当我输入一个地方时,它会为他们生成随机天气并显示他们的输入.

由于最近开始学习 android 开发,所以除了网上的例子我没有尝试太多。

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

/**
 * Implementation for the main activity
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // member variable for the user provided location
    private String location;

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

        // get the Get Forecast button
        Button btnGetForecast = (Button) findViewById(R.id.btnGetForecast);

        // set the click listener to the btnGetForecast Button
        btnGetForecast.setOnClickListener(this);

        EditText loc = findViewById(R.id.etLocationInput);
        location = loc.getText().toString();


    }

    @Override
    public void onClick(View view) {
        // view is the View (Button, ExitText, TextView, etc) that was clicked


        // if it was the btnGetForecast
        if (view.getId() == R.id.btnGetForecast){

        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvTitle" />

    <TextView
        android:id="@+id/tvInstructions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter a location below for the forecast" />

    <EditText
        android:id="@+id/etLocationInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/btnGetForecast"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Forecast" />

    <TextView
        android:id="@+id/tvLocationDisplay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="The weather in " />

</LinearLayout>

我希望该应用程序显示来自用户的输入并将其显示在文本视图中

onCreate 中的这一行:'location = loc.getText().toString()' 只会获取 loc 中的当前文本,在 onCreate 中将是一个空字符串。

您应该查看 TextWatcher: https://developer.android.com/reference/android/text/TextWatcher 这将允许您在 loc 的文本更改时获得回调,然后您可以执行天气生成。

好吧,您需要做的就是在 onClick 方法中添加此代码:

@Override
public void onClick(View view) {
    // view is the View (Button, ExitText, TextView, etc) that was clicked


    // if it was the btnGetForecast
    if (view.getId() == R.id.btnGetForecast){
        String text = loc.getText().toString();
        yourTextView.setText(text);
    }
}

并且不要忘记将视图变量设为全局变量,这样您就可以在 onCreate 方法之外访问它们,而无需再次使用 findViewById()。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private TextView yourTextView;
private EditText loc;

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

    // get the Get Forecast button
    Button btnGetForecast = (Button) findViewById(R.id.btnGetForecast);

    // set the click listener to the btnGetForecast Button
    btnGetForecast.setOnClickListener(this);

    loc = findViewById(R.id.etLocationInput);
    yourTextView = findViewById(R.id.tvLocationDisplay);


    }
 ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM