editText.setText仅在一个活动中起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了editText.setText仅在一个活动中起作用相关的知识,希望对你有一定的参考价值。
我想为我的笔记应用程序制作模板,我可以将其导入到我写下笔记的班级。当我从notizen_ansehen.java启动我的MainActvity时,一切顺利但是当我从templates.java启动我的MainActvity时,方法setText()只是不起作用
这是我的MainActivity
public class MainActivity extends AppCompatActivity {
EditText etxt;
File ordner;
File t;
String s;
String b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
etxt = (EditText) findViewById(R.id.editText);
if (getIntent().hasExtra("editnote_text") && getIntent().hasExtra("2") && getIntent().hasExtra("3")) {
t = (File) getIntent().getExtras().get("editnote_text");
s = getIntent().getStringExtra("2");
b = getIntent().getStringExtra("3");
etxt.setText(gettextfromfile2(t), TextView.BufferType.EDITABLE);
}
String g = "abc"; //this String is only added for testing
etxt.setText(g);
ordner = new File(Environment.getExternalStorageDirectory(), "test_notizenapp");
if (!ordner.exists()) {
ordner.mkdir();
}
}
@Override
protected void onPause() {
super.onPause();
File notizdat;
if (etxt.getText().length() > 0) {
notizdat = new File(ordner, "Notiz_" + System.currentTimeMillis() + ".txt");
try {
OutputStream ops = new FileOutputStream(notizdat);
ops.write(etxt.getText().toString().getBytes());
ops.close();
etxt.clearComposingText();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "kein Inhalt", Toast.LENGTH_SHORT).show();
}
}
public String gettextfromfile2(File datei) {
StringBuilder strgbuilder = new StringBuilder();
try {
BufferedReader buffread = new BufferedReader(new FileReader(datei));
String zeile;
while ((zeile = buffread.readLine()) != null) {
strgbuilder.append(zeile);
strgbuilder.append("\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strgbuilder.toString().trim();
}
}
我的layout.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.example.ludwig.mynote.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="360dp"
android:layout_height="293dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.039"
app:layout_constraintHorizontal_bias="0.6"
tools:layout_editor_absoluteY="16dp"
tools:layout_editor_absoluteX="14dp" />
</android.support.constraint.ConstraintLayout>
我真的不知道问题是什么
答案
尝试将您的代码移动到onCreate()
并首先初始化View
然后使用etxt.setText
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etxt = (EditText) findViewById(R.id.editText);
if (getIntent().hasExtra("editnote_text") && getIntent().hasExtra("2") &&
getIntent().hasExtra("3")) {
t = (File) getIntent().getExtras().get("editnote_text");
s = getIntent().getStringExtra("2");
b = getIntent().getStringExtra("3");
etxt.setText(gettextfromfile2(t), TextView.BufferType.EDITABLE);
}
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
String g = "abc"; //this String is only added for testing
etxt.setText(g);
ordner = new File(Environment.getExternalStorageDirectory(), "test_notizenapp");
if (!ordner.exists()) {
ordner.mkdir();
}
}
另一答案
您需要在上面初始化此行 - :
@Override
protected void onStart() {
super.onStart();
etxt = (EditText) findViewById(R.id.editText);
if (getIntent().hasExtra("editnote_text") && getIntent().hasExtra("2") && getIntent().hasExtra("3")) {
t = (File) getIntent().getExtras().get("editnote_text");
s = getIntent().getStringExtra("2");
b = getIntent().getStringExtra("3");
etxt.setText(gettextfromfile2(t), TextView.BufferType.EDITABLE);
}
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
String g = "abc"; //this String is only added for testing
etxt.setText(g);
ordner = new File(Environment.getExternalStorageDirectory(), "test_notizenapp");
if (!ordner.exists()) {
ordner.mkdir();
}
}
因为光标进入getIntent方法并且没有找到edittext所以在启动时初始化它
以上是关于editText.setText仅在一个活动中起作用的主要内容,如果未能解决你的问题,请参考以下文章
edittext.settext() 将键盘类型更改为默认值 [从 ?123 到 ABC]
EditText.SetText() 在自定义适配器中更改我的软键盘输入类型