【发布时间】:2014-06-25 17:48:20
【问题描述】:
您好,我已经尝试了几个小时,我想放弃。去过很多教程和在线网站来尝试解决这个问题。我正在尝试将 Edittext 字段中的文本存储到变量中,但我没有任何运气。这是我的xml和java代码
<EditText
android:id="@+id/emailTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/uc_logo"
android:layout_alignParentLeft="true"
android:layout_below="@+id/uc_logo"
android:ems="10"
android:hint="@string/email_hint"
android:inputType="textEmailAddress"
android:textColorHint="#000000"
android:textStyle="italic" />
<EditText
android:id="@+id/passTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/emailTxt"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="@string/password_hint"
android:inputType="textPassword"
android:textColorHint="#000000"
android:textStyle="italic" />
代码:
public class MainActivity extends ActionBarActivity {
//variables
/*String login_test_name = "t";
String login_test_pass = "t";
EditText username , pass;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Remove action bar from screen
setContentView(R.layout.activity_main);
//get EditText field data by id name
/*username = (EditText)findViewById(R.id.emailTxt);
pass = (EditText)findViewById(R.id.passTxt);*/
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//code to change activity with BTN click (to register user)
public void registerUser(View v)
{
Intent i = new Intent(MainActivity.this, UserRegister.class );
startActivity(i);
}
// code to change activity (to order menu if username and pass are ture)
public void orderMenu(View v)
{
//variables
String login_test_name = "t";
String login_test_pass = "t";
EditText username , pass;
//get EditText field data by id name
username = (EditText)findViewById(R.id.emailTxt);
pass = (EditText)findViewById(R.id.passTxt);
String u = username.getText().toString(); // change EditText type to String type
String p = pass.getText().toString();
if (!u.equals(login_test_name) && !p.equals(login_test_pass))
{
Toast.makeText(this, "wrong username or password", Toast.LENGTH_SHORT).show();
Toast.makeText(this, u, Toast.LENGTH_SHORT).show();
Toast.makeText(this, p, Toast.LENGTH_SHORT).show();
Toast.makeText(this, login_test_name, Toast.LENGTH_SHORT).show();
Toast.makeText(this, login_test_pass, Toast.LENGTH_SHORT).show();
}
else
{
/*
* Intent i = new Intent(MainActivity.this, OrderMenu.class );
* startActivity(i);
*/
Toast.makeText(this, "logged in", Toast.LENGTH_SHORT).show();
}
}//end order menu method
【问题讨论】:
-
你从哪里调用 orderMenu() 方法
-
ops 对不起,我的 xml 中有它....
-
从edittext得到的u和p的值是多少?
-
我在两者中都输入了一个“t”来测试它。它每次都会进入 if 部分并告诉我我的通行证或用户名错误。当我使用 toast 从编辑文本字段中查看变量中的内容时,它们是空的