0%

安卓开发练习1

安卓开发练习

(学的很菜,不具有参考价值)

用户登录对话框

设计一个按钮,按一下弹出对话框,在对话框里用户可以输入用户名和密码,按确定后可以显示用户名和密码。

new_layout.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/ll_1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_1"
android:text=""
android:hint="请输入你的用户名">
</EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_2"
android:text=""
android:hint="请输入你的密码"
android:inputType="textPassword">
</EditText>
</LinearLayout>

MainActivity文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.myapplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
String yhm,mm;
EditText et_1,et_2;
TextView tv_1;
LinearLayout ll_1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnstart=findViewById(R.id.bt_1);
ll_1=findViewById(R.id.ll_1);
tv_1=findViewById(R.id.tv_1);
String a=tv_1.getText().toString();
final LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View view = factory.inflate(R.layout.new_layout, null);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder myDialog=new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("提示");
myDialog.setMessage("这是一个AlertDialog对话框");
myDialog.setView(view);
myDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
et_1=view.findViewById(R.id.et_1);
yhm=et_1.getText().toString();
et_2=view.findViewById(R.id.et_2);
mm=et_2.getText().toString();
Toast.makeText(MainActivity.this,"用户名是:"+yhm+"\n密码是:"+mm,Toast.LENGTH_SHORT).show();
}
});
myDialog.setNegativeButton("取消",null);
myDialog.show();
}
});
}
}

省市列表

第一个列表选择省份,第二个列表选择具体城市。
xml文档如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Spinner
android:layout_width="50pt"
android:layout_height="wrap_content"
android:id="@+id/sp_1">
</Spinner>
<Spinner
android:layout_width="50pt"
android:layout_height="wrap_content"
android:id="@+id/sp_2">
</Spinner>
</LinearLayout>

activity如下:(本来应该用setOnItemClickedListener的,但用了会闪退,并不知道为什么)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class MainActivity extends Activity {
private String myProvince,myCity;
private Spinner sp_1,sp_2;
private String []provinces=new String[]{"辽宁","吉林","黑龙江","河北","山西","陕西","甘肃","青海","山东","安徽","江苏","浙江","河南","湖北","湖南","江西","台湾","福建","云南","海南","四川","贵州","广东","内蒙古","新疆","广西","西藏","宁夏","北京","上海","天津","重庆","香港","澳门"};
private String [][]cities=new String[][]{{"沈阳","大连","鞍山","抚顺","本溪","丹东","锦州","营口","阜新","辽阳","盘锦","铁岭","朝阳","葫芦岛"}
,{"长春","吉林","四平","辽源","通化","白山","松原","白城","延边"},{"哈尔滨","齐齐哈尔","鸡西","鹤岗","双鸭山","大庆","伊春","佳木斯","七台河","牡丹江","黑河","绥化","大兴安岭"}
,{"石家庄","唐山","秦皇岛","邯郸","邢台","保定","张家口","承德","沧州","廊坊","衡水"}
,{"太原","大同","阳泉","长治","晋城","朔州","晋中","运城","忻州","临汾","吕梁"}
,{"西安","铜川","宝鸡","咸阳","渭南","延安","汉中","榆林","安康","商洛"}
,{"兰州","嘉峪关","金昌","白银","天水","武威","张掖","平凉","酒泉","庆阳","定西","陇南","临夏","甘南"}
,{"西宁","海东","海北","黄南","海南","果洛","玉树","海西"}
,{"济南","青岛","淄博","枣庄","东营","烟台","潍坊","威海","济宁","泰安","日照","莱芜","临沂","德州","聊城","滨州","菏泽"}
,{"合肥","芜湖","蚌埠","淮南","马鞍山","淮北","铜陵","安庆","黄山","滁州","阜阳","宿州","巢湖","六安","亳州","池州","宣城"}
,{"南京","无锡","徐州","常州","苏州","南通","连云港","淮安","盐城","扬州","镇江","泰州","宿迁"}
,{"杭州","宁波","温州","嘉兴","湖州","绍兴","金华","衢州","舟山","台州","丽水"}
,{"郑州","开封","洛阳","平顶山","焦作","鹤壁","新乡","安阳","濮阳","许昌","漯河","三门峡","南阳","商丘","信阳","周口","驻马店"}
,{"武汉","黄石","襄樊","十堰","荆州","宜昌","荆门","鄂州","孝感","黄冈","咸宁","随州","恩施"}
,{"长沙","株洲","湘潭","衡阳","邵阳","岳阳","常德","张家界","益阳","郴州","永州","怀化","娄底","湘西"}
,{"南昌","景德镇","萍乡","九江","新余","鹰潭","赣州","吉安","宜春","抚州","上饶"}
,{"台北","高雄","基隆","台中","台南","新竹","嘉义"}
,{"福州","厦门","莆田","三明","泉州","漳州","南平","龙岩","宁德"}
,{"昆明","曲靖","玉溪","保山","昭通","丽江","普洱","临沧","文山","红河","西双版纳","楚雄","大理","德宏","怒江","迪庆"}
,{"海口","三亚"}
,{"成都","自贡","攀枝花","泸州","德阳","绵阳","广元","遂宁","内江","乐山","南充","宜宾","广安","达州","眉山","雅安","巴中","资阳","阿坝","凉山"}
,{"贵阳","六盘水","遵义","安顺","铜仁","毕节","黔西南","黔东南","黔南"}
,{"广州","深圳","珠海","汕头","韶关","佛山","江门","湛江","茂名","肇庆","惠州","梅州","汕尾","河源","阳江","清远","东莞","中山","潮州","揭阳","云浮"}
,{"呼和浩特","包头","乌海","赤峰","通辽","鄂尔多斯","呼伦贝尔","巴彦淖尔","乌兰察布","兴安","锡林郭勒","阿拉善"}
,{"乌鲁木齐","克拉玛依","吐鲁番","哈密","和田","阿克苏","喀什","克孜勒苏柯尔克孜","巴音郭楞蒙古","昌吉","博尔塔拉蒙古","伊犁哈萨克","塔城","阿勒泰"}
,{"南宁","柳州","桂林","梧州","北海","防城港","钦州","贵港","玉林","百色","贺州","河池","来宾","崇左"}
,{"拉萨","昌都","山南","那曲","阿里","林芝"}
,{"银川","石嘴山","吴忠","固原","中卫"}
,{},{},{},{},{},{}};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp_1=findViewById(R.id.sp_1);
sp_2=findViewById(R.id.sp_2);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,provinces);
sp_1.setAdapter(adapter);
sp_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayAdapter<String>adapterr=new ArrayAdapter<String>(MainActivity.this,R.layout.list_item,cities[position]);
sp_2.setAdapter(adapterr);
myProvince=((TextView)view).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
sp_2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
myCity=((TextView)view).getText().toString();
Toast.makeText(MainActivity.this,"你选择了: "+myProvince+"省"+myCity+"市",Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}
};

动态下拉列表

用一个EditText来跟用户交互。用户按下”添加”按钮就把EditText的内容添加到下拉列表里,按下”删除”按钮就降下拉列表里EditText的内容删除(如果有的话)。
xml文档如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_1"
android:hint="请输入…">
</EditText>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/sp_1">
</Spinner>

<Button
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"></Button>

<Button
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"></Button>

</LinearLayout>

activity_main文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class MainActivity extends Activity {
private Button bt_1,bt_2;
private EditText et_1;
private Spinner sp_1;
private ArrayList<String> strings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_1=findViewById(R.id.et_1);
bt_1=findViewById(R.id.bt_1);
bt_2=findViewById(R.id.bt_2);
sp_1=findViewById(R.id.sp_1);
strings=new ArrayList<String>();
bt_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tem=et_1.getText().toString();
strings.add(tem);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,strings);
sp_1.setAdapter(adapter);
Toast.makeText(MainActivity.this,"添加成功!",Toast.LENGTH_SHORT).show();
}
});
bt_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tem=et_1.getText().toString();
if(strings.contains(tem)){
strings.remove(tem);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,strings);
sp_1.setAdapter(adapter);
Toast.makeText(MainActivity.this,"删除成功!",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"删除失败!",Toast.LENGTH_SHORT).show();
}
}
});
}
};