界面设计-基本组件
【步骤】
<resources>
<string-array name="spingarr">
<item>人力资源部item>
<item>销售部item>
<item>财务部item>
<item>开发item>
string-array>
resources>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList <String> fav;
String sex;
String item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
final String items[] = getResources().getStringArray(R.array.spingarr);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
item = items[position];
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
item =items[0];
}
});
}
public void myclick(View view) {
final EditText ed1 = (EditText)findViewById(R.id.ed1);
final EditText ed2 = (EditText)findViewById(R.id.ed2);
final EditText ed3 = (EditText)findViewById(R.id.ed3);
String name= ed1.getText().toString();
String pass = ed2.getText().toString();
String call = ed3.getText().toString();
getSex();
getFavorite();
if(name.isEmpty()||pass.isEmpty()||call.isEmpty()||sex.isEmpty()||fav.isEmpty()){
Toast.makeText(this,"请检查输入",Toast.LENGTH_SHORT).show();
}
else {
String info = name+";\n"+pass+";\n"+sex+";\n"+call+";\n"+item+";\n"+fav;
Toast.makeText(this,name+";"+pass+";"+sex+";"+call+";"+item+";"+fav,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,ResultActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("info",info);
intent.putExtras(bundle);
startActivity(intent);
}
}
public void getSex(){
final RadioGroup group = (RadioGroup)findViewById(R.id.group1);
for(int i = 0; i<group.getChildCount(); i++){
RadioButton sexs = (RadioButton)group.getChildAt(i);
if (sexs.isChecked()){
sex = (String) sexs.getText();
break;
}
}
}
public void getFavorite(){
final CheckBox like1 = (CheckBox)findViewById(R.id.like1);
final CheckBox like2 = (CheckBox)findViewById(R.id.like2);
final CheckBox like3 = (CheckBox)findViewById(R.id.like3);
final CheckBox like4 = (CheckBox)findViewById(R.id.like4);
fav = new ArrayList<String>();
if (like1.isChecked()){
fav.add(like1.getText().toString());
}
if (like2.isChecked()){
fav.add(like2.getText().toString());
}
if (like3.isChecked()){
fav.add(like3.getText().toString());
}
if (like4.isChecked()){
fav.add(like4.getText().toString());
}
}
}
ResultActivity.java
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Button button2 = (Button)findViewById(R.id.button2);
TextView textView = (TextView)findViewById(R.id.textView);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String mm = bundle.getString("info");
textView.setText(mm);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="20dp" />
<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="20dp" />
<EditText
android:id="@+id/ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="20dp" />
<RadioGroup
android:id="@+id/group1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/sex1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="男" />
<RadioButton
android:id="@+id/sex2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="女" />
RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系电话"
android:textSize="20dp" />
<EditText
android:id="@+id/ed3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:phoneNumber="true"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="部门"
android:textSize="20dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:entries="@array/spingarr"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">
<CheckBox
android:id="@+id/like1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="书籍" />
<CheckBox
android:id="@+id/like2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="运动" />
<CheckBox
android:id="@+id/like3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="音乐" />
<CheckBox
android:id="@+id/like4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="电影" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myclick"
android:text="提交" />
LinearLayout>
LinearLayout>
LinearLayout>
Activity_result.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bean.myapplication48.ResultActivity">
<TextView
android:id="@+id/textView"
android:layout_width="299dp"
android:layout_height="194dp"
android:text="TextView"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.175" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.652" />
android.support.constraint.ConstraintLayout>
动态数组添加和重复问题
使用ArrayList fav定义,通过fav.add( );追加元素。
每次调用之前,fav重新初始化,避免重复。
输入检查
采用isEmpty()方法判断是否为空,所有输入都要判断
Intent传值
Intent要传的值比较多,为了方便起见,可以把需要的信息合并为一个String后再进行传递。