【问题标题】:editText.setText only function from one activityeditText.setText 仅适用于一项活动
【发布时间】:2018-06-13 21:34:07
【问题描述】:

我想为我的笔记应用程序制作模板,我可以将其导入到我写笔记的班级。当我从 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>

我真的不知道问题出在哪里

【问题讨论】:

    标签: java android android-activity android-edittext


    【解决方案1】:

    试试这个把你的代码移动到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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要在上面插入这一行-:

      @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所以在启动时将其引入

      【讨论】:

      • 我已经按照你说的编辑了它,但它仍然不起作用。你知道问题可能是什么吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2021-02-10
      • 2021-02-12
      • 1970-01-01
      相关资源
      最近更新 更多