用户界面
代码如下:
主要有两个界面;第一个是主页面,第二个是定义的界面(名字自己定义)
主界面:
public class MainActivity extends Activity {
private EditText nameEditText;//账号
private EditText pwsEditText;//密码
private RadioGroup sexRadioGroup;//性别
private EditText pwsEditText;//密码
private RadioGroup sexRadioGroup;//性别
//定义爱好的三个控件
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;
private Button okButton;//确定
private Button resButton;//取消
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义账号,密码,性别,爱好,确定,取消的findViewById;
nameEditText=(EditText) findViewById(R.id.editText1);
pwsEditText=(EditText) findViewById(R.id.editText2);
sexRadioGroup=(RadioGroup) findViewById(R.id.radioGroup1);
checkBox1=(CheckBox) findViewById(R.id.checkBox1);
checkBox2=(CheckBox) findViewById(R.id.checkBox2);
checkBox3=(CheckBox) findViewById(R.id.checkBox3);
okButton=(Button) findViewById(R.id.button1);
resButton=(Button) findViewById(R.id.button2);
//实例化确定按钮
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//帐号
String string=nameEditText.getText().toString();
//密码
String str=pwsEditText.getText().toString();
if (string.equals("admin")&& str.equals("123456")) {
Toast.makeText(getApplicationContext(), "登陆成功!", Toast.LENGTH_SHORT).show();
//登陆时所需要的程序代码
Intent intent=new Intent();
intent.putExtra("name", nameEditText.getText().toString());//账号
intent.putExtra("pws", pwsEditText.getText().toString());//密码
int id=sexRadioGroup.getCheckedRadioButtonId();//性别
RadioButton rbButton=(RadioButton) findViewById(id);
intent.putExtra("sex", rbButton.getText().toString());
String hot="";//爱好
if (checkBox1.isChecked()) {
hot+=checkBox1.getText().toString()+",";
}
if (checkBox2.isChecked()) {
hot+=checkBox2.getText().toString()+",";
}
if (checkBox3.isChecked()) {
hot+=checkBox3.getText().toString();
}
intent.putExtra("hot", hot);
intent.setClass(MainActivity.this, One.class);
startActivity(intent);
sendBroadcast(intent);//跳转页面
}
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;
private Button okButton;//确定
private Button resButton;//取消
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义账号,密码,性别,爱好,确定,取消的findViewById;
nameEditText=(EditText) findViewById(R.id.editText1);
pwsEditText=(EditText) findViewById(R.id.editText2);
sexRadioGroup=(RadioGroup) findViewById(R.id.radioGroup1);
checkBox1=(CheckBox) findViewById(R.id.checkBox1);
checkBox2=(CheckBox) findViewById(R.id.checkBox2);
checkBox3=(CheckBox) findViewById(R.id.checkBox3);
okButton=(Button) findViewById(R.id.button1);
resButton=(Button) findViewById(R.id.button2);
//实例化确定按钮
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//帐号
String string=nameEditText.getText().toString();
//密码
String str=pwsEditText.getText().toString();
if (string.equals("admin")&& str.equals("123456")) {
Toast.makeText(getApplicationContext(), "登陆成功!", Toast.LENGTH_SHORT).show();
//登陆时所需要的程序代码
Intent intent=new Intent();
intent.putExtra("name", nameEditText.getText().toString());//账号
intent.putExtra("pws", pwsEditText.getText().toString());//密码
int id=sexRadioGroup.getCheckedRadioButtonId();//性别
RadioButton rbButton=(RadioButton) findViewById(id);
intent.putExtra("sex", rbButton.getText().toString());
String hot="";//爱好
if (checkBox1.isChecked()) {
hot+=checkBox1.getText().toString()+",";
}
if (checkBox2.isChecked()) {
hot+=checkBox2.getText().toString()+",";
}
if (checkBox3.isChecked()) {
hot+=checkBox3.getText().toString();
}
intent.putExtra("hot", hot);
intent.setClass(MainActivity.this, One.class);
startActivity(intent);
sendBroadcast(intent);//跳转页面
}
//输入信息错误的提示信息
else {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("提示信息");
builder.setMessage("你输入的账号或密码错误!");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
nameEditText.setText("");
pwsEditText.setText("");
}
});
else {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("提示信息");
builder.setMessage("你输入的账号或密码错误!");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
nameEditText.setText("");
pwsEditText.setText("");
}
});
//取消按钮退出
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
builder.create();
builder.show();
}
}
});
resButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
builder.create();
builder.show();
}
}
});
resButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
@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;
}
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;
}
}
//第二个界面自己定义的界面
public class One extends Activity {
private TextView textView1;
private TextView textView2;
private TextView textView3;
private TextView textView4;
private Button btnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
Intent intent=getIntent();
textView1=(TextView) findViewById(R.id.textView1);
textView1.setText("帐号:"+intent.getStringExtra("name"));
textView2=(TextView) findViewById(R.id.textView2);
textView2.setText("密码:"+intent.getStringExtra("pws"));
textView3=(TextView) findViewById(R.id.textView3);
textView3.setText("性别:"+intent.getStringExtra("sex"));
textView4=(TextView) findViewById(R.id.textView4);
textView4.setText("爱好:"+intent.getStringExtra("hot"));
btnButton=(Button) findViewById(R.id.buttonup);
btnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
private TextView textView1;
private TextView textView2;
private TextView textView3;
private TextView textView4;
private Button btnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
Intent intent=getIntent();
textView1=(TextView) findViewById(R.id.textView1);
textView1.setText("帐号:"+intent.getStringExtra("name"));
textView2=(TextView) findViewById(R.id.textView2);
textView2.setText("密码:"+intent.getStringExtra("pws"));
textView3=(TextView) findViewById(R.id.textView3);
textView3.setText("性别:"+intent.getStringExtra("sex"));
textView4=(TextView) findViewById(R.id.textView4);
textView4.setText("爱好:"+intent.getStringExtra("hot"));
btnButton=(Button) findViewById(R.id.buttonup);
btnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
@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;
}
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;
}
}
//在AndroidManifest中更改跳转页面的界面
<activity
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.test.Other"
android:label="@string/app_name" >
</activity>
注意:程序的页面也要调整,按照要求进行调整页面;在第二个页面中要更改 (setContentView(R.layout.one);)这句;控件要在“LinearLayout”中写。
</intent-filter>
</activity>
<activity
android:name="com.example.test.Other"
android:label="@string/app_name" >
</activity>
注意:程序的页面也要调整,按照要求进行调整页面;在第二个页面中要更改 (setContentView(R.layout.one);)这句;控件要在“LinearLayout”中写。
页面控件代码:第一个页面
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帐号:"
android:textSize="20sp" />
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帐号:"
android:textSize="20sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" >
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</LinearLayout>
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20sp" />
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="20sp" />
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="20sp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:textSize="20sp" />
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:textSize="20sp" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="足球" />
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="足球" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="篮球" />
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="篮球" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="乒乓" />
</LinearLayout>
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="乒乓" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:gravity="center" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>
第二个页面:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/buttonup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" />
android:id="@+id/buttonup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" />
</LinearLayout>