目标:
当您按下按钮时,应在textview中显示带有新文本的新行。
问题:
基于源代码,它无法实现目标。
我错过了哪一部分?
的信息:
*我是android的新手
*我使用的是API 23
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.netvork">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.jfdimarzio.netvork.MainActivity">
<Button
android:id="@+id/btn_send"
android:layout_width="349dp"
android:layout_height="54dp"
android:layout_alignEnd="@+id/edittxt_input"
android:layout_alignParentBottom="true"
android:layout_marginBottom="12dp"
android:onClick="inputData"
android:text="Send"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="441dp" />
<EditText
android:id="@+id/edittxt_input"
android:layout_width="348dp"
android:layout_height="39dp"
android:layout_above="@+id/btn_send"
android:layout_alignStart="@+id/txtview_display"
android:layout_marginBottom="19dp"
android:ems="10"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="392dp" />
<TextView
android:id="@+id/txtview_display"
android:layout_width="348dp"
android:layout_height="355dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
</RelativeLayout>
package com.jfdimarzio.netvork;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static com.jfdimarzio.netvork.R.id.txtview_display;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void inputData(View view)
{
EditText editText = (EditText) findViewById(R.id.edittxt_input);
String message = editText.getText().toString();
TextView myTextView = (TextView) findViewById(R.id.txtview_display);
myTextView.setText(message + "\n");
}
}
答案 0 :(得分:2)
我认为您希望在新行中显示 editText 的内容以及之前的文本。如果是这样,您可以更改函数 inputData(),如下所示:
public void inputData(View view) {
EditText editText = (EditText) findViewById(R.id.edittxt_input);
String message = editText.getText().toString();
TextView myTextView = (TextView) findViewById(R.id.txtview_display);
String oldText = myTextView.getText();
myTextView.setText(oldText + "\n" + message);
}
答案 1 :(得分:0)
实际上,当你想要从活动中立即使用setText时,你必须刷新Activity,然后由于SharedPreferences未保存的字符串而无法工作。或者您可以创建这样的方法并添加SharedPreferences
public class MainActivity extends AppCompatActivity {
private String message;
private TextView myTextView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.txtview_display);
editText = (EditText) findViewById(R.id.edittxt_input);
}
public void inputData(View view) {
message = editText.getText().toString().trim();
if (!message .isEmpty()){
MethodName(message);
}
}
public void MethodName(String message){
myTextView.setText(message + "\n");
}
}