安卓开发

慕课网 android攻城狮

package com.amaker.test;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.RadioButton;import android.widget.Spinner;import android.widget.ToggleButton;public class MainActivity extends Activity {private Button register,cancel;private ToggleButton marriged;private RadioButton male,female;private EditText username,password;private Spinner position;private CheckBox reading,swimming;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);username = (EditText)findViewById(R.id.username);password = (EditText)findViewById(R.id.password);male = (RadioButton)findViewById(R.id.male);female = (RadioButton)findViewById(R.id.female);reading = (CheckBox)findViewById(R.id.reading);swimming = (CheckBox)findViewById(R.id.swimming);marriged = (ToggleButton)findViewById(R.id.marriged);position = (Spinner)findViewById(R.id.position);String[] str = {“CEO”,”CFO”,”PM”};ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,str);position.setAdapter(aa);register = (Button)findViewById(R.id.register);cancel = (Button)findViewById(R.id.cancel);register.setOnClickListener(new OnClickListener() {public void onClick(View v) {Bundle b = new Bundle();b.putString(“username”, “用户名称:”+username.getText().toString());b.putString(“password”, “用户密码:”+password.getText().toString());if(male.isChecked()){b.putString(“gender”, “性别:男”);}else{b.putString(“gender”, “性别:女”);}String temp = “爱好:”;if(reading.isChecked()){temp+=”阅读”;}if(swimming.isChecked()){temp+=” “;temp+=”游泳”;}b.putString(“hobby”, temp);if(marriged.isChecked()){b.putString(“marriged”, “婚否:已婚”);}else{b.putString(“marriged”, “婚否:未婚”);}b.putString(“position”,”职位:”+ position.getSelectedItem().toString());Intent intent = new Intent(MainActivity.this,ResultActivity.class);intent.putExtra(“data”, b);startActivity(intent);}。

慕课网 android攻城狮

Kotlin使用的布局中的控件id必须使用小驼峰方式, 例: android:id=”@+id/tvLogin”。

android:id属性为Button指定一个唯一标识符,我们在.java文件里获取布局文件里的元素时就需要这个属性。

public void onCreate(Bundle savedInstanceState) {23 super.onCreate(savedInstanceState);4 setContentView(R.layout.activity_main);5 edit1=(EditText)findViewById(R.id.editText1);6 b1=(Button)findViewById(R.id.button1);7 b2=(Button)findViewById(R.id.button2);8 b3=(Button)findViewById(R.id.button3);9 b4=(Button)findViewById(R.id.button4);10 b5=(Button)findViewById(R.id.button5);11 b6=(Button)findViewById(R.id.button6);12 b7=(Button)findViewById(R.id.button7);13 b8=(Button)findViewById(R.id.button8);1415 b1.setOnClickListener(listener);16 b2.setOnClickListener(listener);17 b3.setOnClickListener(listener);18 b4.setOnClickListener(listener);19 b5.setOnClickListener(listener);20 b6.setOnClickListener(listener);21 b7.setOnClickListener(listener);22 b8.setOnClickListener(listener);23242526 }2728 private Button.OnClickListener listener =new29 Button.OnClickListener() {3031 @Override32 public void onClick(View v) {33 // TODO Auto-generated method stub34 switch (v.getId()) {35 case R.id.button1:36 displayATM(“1”);37 break;38 case R.id.button2:39 displayATM(“2”);40 break;41 case R.id.button3:42 displayATM(“3”);43 break;44 case R.id.button4:45 displayATM(“4”);46 break;47 case R.id.button5:48 displayATM(“5”);49 break;50 case R.id.button6:51 displayATM(“6”);52 break;53 case R.id.button7:{54 String str=edit1.getText().toString();55 if (str.length()>0){56 str=str.substring(0,str.length()-1);57 edit1.setText(str);58 }59 }60 case R.id.button8:{61 String str=edit1.getText().toString();62 if (str.equals(“667788”)){63 Toast toast =Toast.makeText(MainActivity.this, “密码正确,欢迎使用提款功能”, Toast.LENGTH_LONG);64 toast.show();65 }66 else{67 Toast toast =Toast.makeText(MainActivity.this, “密码错误,请重新输入”, Toast.LENGTH_LONG);68 toast.show();69 }7071 }72 default:73 break;74 }75 }76 };77 private void displayATM(String s){78 String str=edit1.getText().toString();79 edit1.setText(str+s);80 }。

各个用户名id被定义在数组android_ids[]中,此数组表示了一个映射关系,能够将字符串和整数值对应起来。定义代码如下所示。

Android中所有的资源都有一个对应的资源ID,资源ID的类型为16进制的整数。有些时候特殊的场合处理资源ID,为了调试需要进行进制转换,比如16进制转常用的10进制。不用自己算,使用下面的工具就可以轻松搞定。

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    // 获取三个UI组件    start = (Button)findViewById(R.id.start);    chronoText = (TextView)findViewById(R.id.chronoText);    chronoValue = (EditText)findViewById(R.id.chronoValue);    start.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            // 获取EditText里的数值            int value = Integer.parseInt(String.valueOf(chronoValue.getText()));            // 验证数值是否大于零            if (value > 0) {                new Chronograph().execute(value);            }            else {                Toast.makeText(AsyncTaskActivity.this, “请输入一个大于零的整数值 !”, Toast.LENGTH_LONG).show();            }        }    });}如果我们在继承AsyncTask类时,对于三个参数中有不需要的,可以定义为Void类型(注意,与小写的 void 不同),例如:

第四步,创建地图Activity,管理地图生命周期。

package com.example.imooc_weixinfragment;import android.app.Activity;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.Window;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.LinearLayout;//我们使用的是android v4包下的fragment,这里必须要继承自FragmentActivity,而不是Activitypublic class MainActivity extends FragmentActivity implements OnClickListener{//底部的4个导航控件private LinearLayout mTabWeixin;private LinearLayout mTabFrd;private LinearLayout mTabAddress;private LinearLayout mTabSetting;//底部4个导航控件中的图片按钮private ImageButton mImgWeixin;private ImageButton mImgFrd;private ImageButton mImgAddress;private ImageButton mImgSetting;//初始化4个Fragmentprivate Fragment tab01;private Fragment tab02;private Fragment tab03;private Fragment tab04;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();//初始化所有的viewinitEvents();setSelect(0);//默认显示微信聊天界面}private void initEvents() {mTabWeixin.setOnClickListener(this);mTabFrd.setOnClickListener(this);mTabAddress.setOnClickListener(this);mTabSetting.setOnClickListener(this);}private void initView() {mTabWeixin = (LinearLayout)findViewById(R.id.id_tab_weixin);mTabFrd = (LinearLayout)findViewById(R.id.id_tab_frd);mTabAddress = (LinearLayout)findViewById(R.id.id_tab_address);mTabSetting = (LinearLayout)findViewById(R.id.id_tab_setting);mImgWeixin = (ImageButton)findViewById(R.id.id_tab_weixin_img);mImgFrd = (ImageButton)findViewById(R.id.id_tab_frd_img);mImgAddress = (ImageButton)findViewById(R.id.id_tab_address_img);mImgSetting = (ImageButton)findViewById(R.id.id_tab_setting_img);}@Overridepublic 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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Overridepublic void onClick(View v) {resetImg();switch (v.getId()) {case R.id.id_tab_weixin://当点击微信按钮时,切换图片为亮色,切换fragment为微信聊天界面setSelect(0);break;case R.id.id_tab_frd:setSelect(1);break;case R.id.id_tab_address:setSelect(2);break;case R.id.id_tab_setting:setSelect(3);break;。

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注