从android中的EditText字段创建一个数组

时间:2022-02-12 21:13:02

I need an Array with username and password. The method containing the array needs to return it. I tried the following code but android studio is showing and error when I tried to access the array from another method:

我需要一个用户名和密码的数组。包含数组的方法需要返回它。我尝试了以下代码,但是android studio在尝试从另一种方法访问数组时显示错误:

public class MainActivity extends Activity {

    private static Boolean log_status;

    public  String[] credentials () {
        String Username;
        String Password;
        String login_data;
        EditText editText = (EditText) findViewById(R.id.username);
        EditText editPass = (EditText) findViewById(R.id.password);
        Username = editText.getText().toString();
        Password = editPass.getText().toString();

        String[] log_data= new  String[1];
        log_data[0]= Username;
        log_data[1]=Password;

        return log_data;

    }   

How can I make the log_data array be accessed by another method? Does my code return an array?

如何让另一个方法访问log_data数组?我的代码是否返回一个数组?

4 个解决方案

#1


0  

How can you access first element of array which is limited to 1

如何访问数组中第一个被限制为1的元素

  String[] log_data= new  String[1];
  log_data[0]= Username;
  log_data[1]=Password;

Increase the size of the array to 2

将数组的大小增加到2

  String[] log_data= new  String[2];
  log_data[0]= Username;
  log_data[1]=Password;

#2


0  

String[] log_data= new  String[1];
log_data[0]= Username;
log_data[1]=Password;

Your array size is just 1, not 2.

数组大小是1,而不是2。

Try changing

试着改变

String[] log_data= new  String[2];

Also, if it doesn't solve, can you tell exact error message you are getting ?

而且,如果它不能解决问题,你能告诉你确切的错误信息吗?

#3


0  

You made a few mistakes, please try this.

你犯了几个错误,请试试这个。

EditText password,username;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        password = (EditText)findViewById(R.id.passWord);
        username = (EditText)findViewById(R.id.userName);
    }
    public  String[] LogInfo () {
        String passWord = password.getText().toString();
        String userName = username.getText().toString();

            String[] log_data= new String[2];
            log_data[0] = passWord;
            log_data[1] = userName;

            return log_data;

        }

#4


0  

String[] log_data= new String[1]; creates a Vector with 1 dimension, so I think it should be: String[] log_data= new String[2];

String[]log_data =新的字符串[1];创建一个一维的向量,所以我认为应该是:String[] log_data= new String[2];

#1


0  

How can you access first element of array which is limited to 1

如何访问数组中第一个被限制为1的元素

  String[] log_data= new  String[1];
  log_data[0]= Username;
  log_data[1]=Password;

Increase the size of the array to 2

将数组的大小增加到2

  String[] log_data= new  String[2];
  log_data[0]= Username;
  log_data[1]=Password;

#2


0  

String[] log_data= new  String[1];
log_data[0]= Username;
log_data[1]=Password;

Your array size is just 1, not 2.

数组大小是1,而不是2。

Try changing

试着改变

String[] log_data= new  String[2];

Also, if it doesn't solve, can you tell exact error message you are getting ?

而且,如果它不能解决问题,你能告诉你确切的错误信息吗?

#3


0  

You made a few mistakes, please try this.

你犯了几个错误,请试试这个。

EditText password,username;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        password = (EditText)findViewById(R.id.passWord);
        username = (EditText)findViewById(R.id.userName);
    }
    public  String[] LogInfo () {
        String passWord = password.getText().toString();
        String userName = username.getText().toString();

            String[] log_data= new String[2];
            log_data[0] = passWord;
            log_data[1] = userName;

            return log_data;

        }

#4


0  

String[] log_data= new String[1]; creates a Vector with 1 dimension, so I think it should be: String[] log_data= new String[2];

String[]log_data =新的字符串[1];创建一个一维的向量,所以我认为应该是:String[] log_data= new String[2];