編輯:關於Android編程
1.下拉列表Spinner
1.1.activity_main.xml
Spinner是下拉列表的組件
1.2.MainActivity.class
public class MainActivity extends AppCompatActivity { private Spinner s; String[] data=new String[]{"蘋果","雪梨","西瓜","葡萄","橙子","草莓"}; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv= (TextView) this.findViewById(R.id.tv); s= (Spinner) this.findViewById(R.id.spinner); s.setAdapter(new ArrayAdapters.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,data)); s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { String str=data[position]; tv.setText("最喜歡的水果是:"+str); } @Override public void onNothingSelected(AdapterView parent) { } }); } } (this,android.R.layout.simple_list_item_1,data));android.R.layout.simple_list_item_1是指安卓自帶的下拉列表格式,data是數據源;
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()){..};是下拉列表的監聽
2.日期選擇器
2.1.activity.xml
2.2.DateActivity.class
public class DateActivity extends ActionBarActivity { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date); btn=(Button)this.findViewById(R.id.btndate); tv= (TextView) this.findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new DatePickerDialog(DateActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { tv.setText("您的出生日期是:"+String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth)); } },2000,1,2).show(); } }); } }DatePickerDialog日歷選擇器的對話框,監聽為OnDateSetListener(){..}
3.時間選擇器
3.1.布局
3.2.Java文件
public class DateActivity extends ActionBarActivity { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date); btn=(Button)this.findViewById(R.id.btndate); tv= (TextView) this.findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new TimePickerDialog(DateActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { tv.setText(String.format("%d:%d",hourOfDay,minute)); } //0,0指的是時間,true表示是否為24小時,true為24小時制 },0,0,true).show(); } }); } }
4.單項選擇
4.1.布局
4.2.Java文件
public class SingChoose extends AppCompatActivity { private Button btn; private RadioButton rbD; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sing_choose); rbD= (RadioButton) this.findViewById(R.id.rb4); btn= (Button) this.findViewById(R.id.submit); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(rbD.isChecked()){ Toast.makeText(SingChoose.this,"正確,請加五分",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(SingChoose.this,"錯誤,請減五分",Toast.LENGTH_SHORT).show(); } } }); } }
5.多項選擇
5.1.布局
5.2.Java文件
public class CheckChoose extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private CheckBox cb1,cb2,cb3,cb4,cb5; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.check_choose); tv= (TextView) this.findViewById(R.id.tv); cb1= (CheckBox) this.findViewById(R.id.cb1); cb2= (CheckBox) this.findViewById(R.id.cb2); cb3= (CheckBox) this.findViewById(R.id.cb3); cb4= (CheckBox) this.findViewById(R.id.cb4); cb5= (CheckBox) this.findViewById(R.id.cb5); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); cb4.setOnCheckedChangeListener(this); cb5.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { String str="您喜歡:"; if(cb1.isChecked()){ str+=cb1.getText()+","; } if(cb2.isChecked()){ str+=cb2.getText()+","; } if(cb3.isChecked()){ str+=cb3.getText()+","; } if(cb4.isChecked()){ str+=cb4.getText()+","; } if(cb5.isChecked()){ str+=cb5.getText()+","; } tv.setText(str); } }
概要 當手機Modem狀態改變後會將狀態變化信息通知到上層,通過《Android 4.4 Kitkat Phone工作流程淺析(八)__Phone狀態分析》
可能很多人會感覺Activity很簡單,但是經歷了半年的android開發,我發現我對Activity的的理解還是比較淺顯的,其實Activity並沒有我們想
最近的項目中遇到一個非常頭痛的需求,在Android端錄制視頻的時候動態添加像監控畫面一樣的精確到秒的時間信息,關鍵是,並不是說只在播放器的界面顯示時間就可以了,而是錄制
收藏的Android非常好用的組件或者框架。 android框架 先說兩個網站: http://www.androidviews.net/ 很好的國外開源代碼站,就是訪
B. Little Pony and Harmony Ch