首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextView ContextMenu

TextView ContextMenu
EN

Stack Overflow用户
提问于 2021-02-18 09:41:09
回答 2查看 57关注 0票数 0

我想要创建一个TextView,当我点击它时,一个小菜单就会出现,我可以用它来改变文本的颜色。我做了这个,但是当我尝试点击TextView时,没有显示菜单。我该怎么做才能让它发挥作用?

MainActivity

代码语言:javascript
复制
package com.example.menu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    MenuItem pozadi1,pozadi2,pozadi3,pozadi4;
    RelativeLayout pozadi;
    TextView styl;
    TextView barva;
    MenuItem red,blue,green;
    MenuItem normalni,tucne,kurziva;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pozadi = (RelativeLayout) findViewById(R.id.pozadi);
        pozadi1 = (MenuItem) findViewById(R.id.pozadi1);
        pozadi2 = (MenuItem) findViewById(R.id.pozadi2);
        pozadi3 = (MenuItem) findViewById(R.id.pozadi3);
        pozadi4 = (MenuItem) findViewById(R.id.pozadi4);
        styl = (TextView) findViewById(R.id.styl);
        barva = (TextView) findViewById(R.id.barva);
        red = (MenuItem) findViewById(R.id.red);
        blue = (MenuItem) findViewById(R.id.blue);
        green = (MenuItem) findViewById(R.id.green);
        normalni = (MenuItem) findViewById(R.id.normal);
        tucne = (MenuItem) findViewById(R.id.tucne);
        kurziva = (MenuItem) findViewById(R.id.kurziva);

        registerForContextMenu(barva);

    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        new MenuInflater(this).inflate(R.menu.menubarva,menu);
    }
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.blue:
                barva.setTextColor(Color.BLUE);
                return true;
            case R.id.green:
                barva.setTextColor(Color.GREEN);
                return true;
            case R.id.red:
                barva.setTextColor(Color.RED);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
    //KOD NA ZMĚNU POZADÍ
      //////////////////////////////////////////////////////////////////////////
   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu1,menu);
        return super.onCreateOptionsMenu((Menu) menu);
    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.pozadi1:
                pozadi.setBackgroundResource(R.drawable.pozadi1);break;
            case R.id.pozadi2:
                pozadi.setBackgroundResource(R.drawable.pozadi2);break;
            case R.id.pozadi3:
                pozadi.setBackgroundResource(R.drawable.pozadi3);break;
            case R.id.pozadi4:
                pozadi.setBackgroundResource(R.drawable.pozadi4);break;
        }
        return super.onOptionsItemSelected(item);
    }
    ///////////////////////////////////////////////////////////////////////////////////
}

activity_main.xml。我觉得没问题

代码语言:javascript
复制
<?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"
    android:id="@+id/pozadi">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Změna typu písma"
        android:layout_marginTop="100sp"
        android:layout_centerHorizontal="true"
        android:id="@+id/styl"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Změna barvy písma"
        android:layout_marginTop="250sp"
        android:layout_centerHorizontal="true"
        android:id="@+id/barva"/>

</RelativeLayout>

这是一个带有颜色的XML,同样,我认为没有问题。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/red"
        android:title="Červená"/>

    <item android:id="@+id/blue"
        android:title="ˇModrá"/>

    <item android:id="@+id/green"
        android:title="Zelená"/>
</menu>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-18 10:00:52

这是我的项目:GitHub

看看showSeedInfo()方法。

首先调用关于onClickListener的TextView,然后使用lambda调用您的方法(类似于我的方法),该方法显示已膨胀的布局

代码语言:javascript
复制
private void showSeedInfo() {

    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.seed_read_popup, null);

    Seed seed = loadSettings.getSeed();

    int[] rcmdValues = new int[]{seed.getTemperature()[0], seed.getTemperature()[1],
            seed.getHumidity()[0], seed.getHumidity()[1],
            seed.getLight()[0], seed.getLight()[1]};

    Button button = popupView.findViewById(R.id.updateSeedButton);
    button.setOnClickListener(v -> BluetoothActivity.bltSocket.updateSeed(rcmdValues));

    TextView name = popupView.findViewById(R.id.namePlantView);
    name.setText(seed.getNamePlant());
    TextView temperature = popupView.findViewById(R.id.temperaturePlantView);
    temperature.setText(rcmdValues[0] + "-" + rcmdValues[1]);
    TextView humidity = popupView.findViewById(R.id.humidityPlantView);
    humidity.setText(rcmdValues[2] + "-" + rcmdValues[3]);
    TextView light = popupView.findViewById(R.id.lightPlantView);
    light.setText(rcmdValues[4] + "-" + rcmdValues[5]);

    int size = ViewGroup.LayoutParams.WRAP_CONTENT;
    final PopupWindow popupWindow = new PopupWindow(popupView, size, size, true);
    popupWindow.showAtLocation(popupView, Gravity.CENTER, size, size);
}
票数 0
EN

Stack Overflow用户

发布于 2021-02-18 09:47:19

我认为您可以尝试onClickListener public View.OnClickListener mClickListener = new View.OnClickListener() public void onClick(View v) } };

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66257268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档