各式各样的Menu
1.长按一个TextView弹出一个上下文菜单,菜单项有红色/绿色/蓝色。当点击对应的菜单项的时候更改文字颜色。
评分标准:
(1)创建上下文菜单(20分)
(2)实现菜单项的点击(20分)
(3)更改文字颜色(20分)
(4)运行效果(20分)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/red" android:title="红色" app:showAsAction="never"/>
<item android:id="@+id/blue" android:title="绿色" app:showAsAction="never"/>
<item android:id="@+id/green" android:title="蓝色" app:showAsAction="never"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<TextView
android:id="@+id/text_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="..."/>
</RelativeLayout>
package com.example.day2;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text_View);
registerForContextMenu(textView);
}
//加载资源文件
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.option,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
//设置事件监听
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.blue:
textView.setTextColor(Color.parseColor("#000fff"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#fff000"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#ffff00"));
break;
}
return super.onContextItemSelected(item);
}
}
2.创建一个系统菜单,菜单项有红色/绿色/蓝色。当点击对应的菜单项的时候更改Activity背景颜色。
(1)创建系统菜单(20分)
(2)实现菜单项的点击(20分)
(3)更改背景颜色(20分)
(4)运行效果(20分
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Main2Activity">
<TextView
android:id="@+id/te"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="48341351"/>
</LinearLayout>
package com.example.day2;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = findViewById(R.id.te);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.option,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.blue:
textView.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#A21C31"));
break;
}
return super.onOptionsItemSelected(item);
}
}
3.点击textview弹出popupMenu,菜单项有红色/绿色/蓝色。当点击对应的菜单项的时候更改文字内容。
(1)创建弹出菜单popupMenu(20分)
(2)实现菜单项的点击(20分)
(3)更改文字内容(20分)
(4)运行效果(20分)
package com.example.day2;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.TextView;
public class Main3Activity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
textView = findViewById(R.id.text_view1);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu();
}
});
}
private void showPopupMenu() {
PopupMenu popupMenu = new PopupMenu(Main3Activity.this,textView);
popupMenu.inflate(R.menu.option);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.blue:
textView.setTextColor(Color.parseColor("#000000"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#000000"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#000000"));
break;
}
return false;
}
});
popupMenu.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Main3Activity">
<TextView
android:id="@+id/text_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="弹出菜单哟"/>
</RelativeLayout>