所以,我在这里要做的是有一个主要活动,如果满足要求(正确的用户名、密码和男性单选按钮被选中而不是女性),VM 将切换到“成功”活动。如果它不满足这 3 个要求中的任何一个,VM 会在按下按钮时切换到“失败”活动。除了单选按钮外,我让它正常工作。
我在布局上创建了一个 RadioGroup,但我不确定如何在类本身中实现它。我假设您必须找到 ID、覆盖侦听器等……但它无法正常工作。有任何想法吗?我在发布之前取出了大部分 RadioGroup 属性,这样就不会那么混乱了。
主要活动
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener
{
Button button;
EditText login;
EditText password;
RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
login =(EditText)findViewById(R.id.editText1);
password =(EditText)findViewById(R.id.editText2);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String L,P;
L = login.getText().toString();
P = password.getText().toString();
if(L.equals("name") && P.equals("123456"))
{
Intent intent = new Intent();
intent.setClass(MainActivity.this,Welcome.class);
startActivity(intent);
}
else
{
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this,Failed.class);
startActivity(intent1);
}
}
});
}
/* public void onRadioButtonClicked(View view)
{
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId())
{
case R.id.radio1:
if (checked)
{
Intent intent4 = new Intent();
intent4.setClass(MainActivity.this,Welcome.class);
startActivity(intent4);
}
break;
case R.id.radio0:
if (checked)
{
Intent intent2 = new Intent();
intent2.setClass(MainActivity.this,Failed.class);
startActivity(intent2);
}
break;
}
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stub
}
}
失败的活动
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Failed extends Activity
{
Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.failed);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent3 = new Intent();
intent3.setClass(Failed.this,MainActivity.class);
startActivity(intent3);
}
});
}
}
成功活动
import android.app.Activity;
import android.os.Bundle;
public class Welcome extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
}
}