我如何从textview获取信息?

时间:2015-06-23 05:54:33

标签: android textview

我正在试图弄清楚如何将用户输入的信息输入到包含userpassword和username的textview框中。我的目标是在用户单击按钮Login时显示来自两个字段的信息。我被困在试图显示信息。

  public class Login {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
     TextView usernameTextView = (TextView) findViewById(R.id.userloginname);
     TextView passwordTextView = (TextView) findViewById(R.id.userpassword);

    TextView neighbourView = new TextView(this);
    neighbourView.setTag(usernameTextView);
    neighbourView.setTag(passwordTextView);

    Button button_test;
    button_test = (Button) findViewById(R.id.btnLogin);

    button_test.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {

            String username = usernameTextView.toString();
            String password = passwordTextView.toString();
            //  Toast.makeText(getApplicationContext(), "password is :" + password +"  " + username + "  ", Toast.LENGTH_LONG).show();
            // System.out.println(username + "" + password);
            Log.i(username + username, password + password);
        }
    });
    // return false;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
  }

我的XML

     <!--  Username Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="@string/username"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="20dip"
        android:singleLine="true"
        android:id="@+id/userloginname"
        android:inputType="text" />
    <!--  Password Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="@string/password"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:password="true"
        android:inputType="textPassword"
        android:id="@+id/userpassword" />
    <!-- Login button -->
    <Button android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/loginButton"/>

3 个答案:

答案 0 :(得分:2)

usernameTextViewpasswordTextView是EditText而不是TextView。

尝试使用

String username = usernameTextView.getText().toString();

String password = passwordTextView.getText().toString();

而不是

String username = usernameTextView.toString();

String password = passwordTextView.toString();

答案 1 :(得分:1)

首先,用户无法在TextView上输入文字。你需要一个EditText。您已在布局文件中使用EditText,因此将其初始化为

 EditText usernameEditText= (EditText) findViewById(R.id.userloginname);
 EditText passwordEditText = (EditText) findViewById(R.id.userpassword);

然后在你的onClick()方法

public void onClick(View v) {

        String username = usernameEditText.getText().toString();
        String password = passwordEditText.getText().toString();
        Toast.makeText(getApplicationContext(), "password is :" + password +"  " + username + "  ", Toast.LENGTH_LONG).show();
        // System.out.println(username + "" + password);
        Log.i(username + username, password + password);
    }

答案 2 :(得分:0)

在这里,您要将TextView转换为错误的字符串

String username = usernameTextView.toString();

使用此语法代替上述语法从Textview获取文本并将其转换为字符串

String username = usernameTextView.getText().toString();