OptionMenu
optionMenu再点击手机的Menu键触发:
OPtionMenu
1.如何触发Meun的显示
点击Menu键
2.如何向menu中添加MenuItem?重写onCreateOptionMenu方法
1.menu.add
2.菜单文件方式
3.选择某个MenuItem如何响应
重写onOptionsItemSelected方法
-----------------------代码如下---------------------------
public class MenuActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
//用来显示选项菜单的方法 :向Menu中添加Intent
public boolean onCreateOptionsMenu(Menu menu) {
//1.纯编码方式 1.组id 2. item id
menu.add(0, 2, 0, "添加");
menu.add(0, 3, 0, "删除");
//2.菜单文件方式
//1.得到菜单加载器对象
MenuInflater menuInflater = getMenuInflater();
//2.加载菜单文件
menuInflater.inflate(R.menu.option_menu, menu);
return super.onCreateOptionsMenu(menu);
}//选项菜单 吐司
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 2:
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
ContextMenu:上下文菜单
1.如何触发Meun的显示
长按某个视图,并view.setOnCreateMenuListener(this)2.如何向menu中添加MenuItem?
重写onCreateOptionMenu方法
1.menu.add
2.菜单文件方式
3.选择某个MenuItem如何响应
重写onOptionsItemSelected方法
根据ItemId去响应
---------------------------代码如下------------------
public class MenuActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
//长按某个视图
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
//1.添加菜单项
menu.add(0, 1, 0, "添加");
menu.add(0, 2, 0, "删除.");
super.onCreateContextMenu(menu, v, menuInfo);
}
public boolean onContextItemSelected(MenuItem item) {switch (item.getItemId()) {
case 1:
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}
-----------------进度条 Component----------------------
布局 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"android:layout_height="fill_parent"
android:orientation="vertical" >
<!--
gravity="center 居中
ProgressBar 默认为 圆形
<LinearLayout
android:id="@+id/ll_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载中..." />
</LinearLayout>
<ProgressBar
android:id="@+id/pb_progress_loading"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="30"
/>
<SeekBar
android:id="@+id/seekBar1_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="功能需求:\n1.滑动下面的滑竿后,上面的进度条会同步\n2.滑动到最大值时,最上面的进度条消失" />
</LinearLayout>
布局 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!--
gravity="center 居中
-->
<LinearLayout
android:id="@+id/ll_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载中..." />
</LinearLayout>
<ProgressBar
android:id="@+id/pb_progress_loading"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="30"
/>
<SeekBar
android:id="@+id/seekBar1_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="功能需求:\n1.滑动下面的滑竿后,上面的进度条会同步\n2.滑动到最大值时,最上面的进度条消失" />
</LinearLayout>
-----------------------测试进度条-----------代码如下---------------
public class ProgressActivity extends Activity {
private LinearLayout ll_loading;
private ProgressBar pb_progress_loading;
private SeekBar seekBar1_loading;
private OnSeekBarChangeListener onSeekBarChangeListener=new OnSeekBarChangeListener() {
//离开滑竿
public void onStopTrackingTouch(SeekBar seekBar) {
Log.e("TAG", "离开滑竿");
//1.得到seekBer的进度
int progress = seekBar1_loading.getProgress();
//2.设置为progress进度
seekBar1_loading.setProgress(progress);
//3.判断是否达到最大值
if (progress == seekBar1_loading.getMax()) {
//如果达到了,设置圆形进度条显示
// ll_loading.setVisibility(View.INVISIBLE);//不可见但是占空间
ll_loading.setVisibility(View.GONE);//不可见不占空间
}else{
//如果没达到,设置圆形进度条不可见
ll_loading.setVisibility(View.VISIBLE);
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
Log.e("TAG", "按下滑竿");
}
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
Log.e("TAG", "滑动滑竿");
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
ll_loading=(LinearLayout) findViewById(R.id.ll_loading);
pb_progress_loading=(ProgressBar) findViewById(R.id.pb_progress_loading);
seekBar1_loading=(SeekBar) findViewById(R.id.seekBar1_loading);
//给seekBar1设置监听
seekBar1_loading.setOnSeekBarChangeListener(onSeekBarChangeListener);
}