代码之家  ›  专栏  ›  技术社区  ›  Vítek Peterka

编辑文本。getText()。getString()始终为空[关闭]

  •  -7
  • Vítek Peterka  · 技术社区  · 7 年前

    我正在为android开发notes应用程序。我正在尝试将用户为EditText编写的文本保存到变量中 inputText .但变量总是 "" .我真的很困惑。有什么想法吗?(问题可能出在 Setphase2 方法。)

    package vitosoft.notes;
    
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.InputType;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        final int[] phase = {1};
        final String[] text_encode = {""};
        final Button r_button = findViewById(R.id.rightButton);
        final Button l_button = findViewById(R.id.leftButton);
        final EditText input = findViewById(R.id.editText);
        final TextView task = findViewById(R.id.task);
    
        r_button.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            public void onClick(View v) {
                if (phase[0] == 1) {
                    phase[0] = 2;
                    text_encode[0] = Setphase2(r_button, l_button, task, input);
                }
            }
        });
    
        l_button.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            public void onClick(View v) {
                if (phase[0] == 2) {
                    phase[0] = 1;
                    Setphase1(text_encode[0], r_button, l_button, task, input);
    
                }
            }
        });
        }
    
    public String Setphase2(Button r,Button l,TextView t,EditText i) {
        i.setText("");
        r.setText(R.string.upload);
        l.setText(R.string.edit);
        t.setText(R.string.key_task);
        String inputText = i.getText().toString();
        Log.d("UserText", "Entry text is:" + inputText);
        i.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        return inputText;
    }
    public void Setphase1 (String original, Button r,Button l,TextView t,EditText i){
        i.setText(original);
        r.setText(R.string.encode);
        l.setText(R.string.decode);
        t.setText(R.string.entry_task);
        i.setInputType(InputType.TYPE_CLASS_TEXT);
    
    }
    }
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Bhuvanesh BS    7 年前

    Android edittext。getText()。toString()始终为空。。因为您将其指定为空。

    public String Setphase2(Button r,Button l,TextView t,EditText i) {
        i.setText(""); // See this line**************
        r.setText(R.string.upload);
        l.setText(R.string.edit);
        t.setText(R.string.key_task);
        String inputText = i.getText().toString();
        Log.d("UserText", "Entry text is:" + inputText);
        i.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        return inputText;
    }
    

    按上述方法删除以下行。

    i.setText("");
    
        2
  •  1
  •   Guts    7 年前

    您正在调用方法i.setText(“”);在Setphase2方法开始时。

        3
  •  1
  •   NotZack    7 年前

    Setphase2 方法

    i.setText("");
    r.setText(R.string.upload);
    l.setText(R.string.edit);
    t.setText(R.string.key_task);
    String inputText = i.getText().toString();
    

    您正在设置 i "" 然后设置 inputText 等于