【问题标题】:how to get the action bar button? findviewbyid seems doesn't work如何获取操作栏按钮? findviewbyid 似乎不起作用
【发布时间】:2015-08-25 17:34:05
【问题描述】:

假设我有两个按钮,一个是操作栏中的操作按钮(@+id/button1)。另一个是布局中的常用按钮(@+id/button2)。

当我点击button2时如何设置button1禁用?

findViewById(button1) 似乎不起作用。它将返回 null。

这是我的菜单 xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/button1" android:title="submit" android:showAsAction="always" /> </menu>

这是我的主要活动:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.button1 ) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    button1 = (Button) findViewById(R.id.button1);/*which return null*/
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setEnabled(false);/*what i failed to do*/
        }
    });
}

【问题讨论】:

  • 分享你尝试过的代码。

标签: android


【解决方案1】:

1.在你的Activity类中创建一个变量MenuItem

    MenuItem menuItem;

2。在 onCreateOptionsMenu 中找到你的变量

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            menuItem = menu.findItem(R.id.item_circuit);
            return super.onCreateOptionsMenu(menu);
        }
  1. 在您的 buttonClick 方法中禁用该项目

     button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItem.setEnabled(false);
                    }
                });
    

【讨论】:

  • 也许我听错了。当我单击 button2(公共按钮)时,我想禁用 button1(操作按钮)
【解决方案2】:

试试这个
public void button1click(View v) { Button button2=(Button)getActionBar().getCustomView().findViewById(R.id.button2); button2.setClickable(false); button2.setEnabled(false); }

【讨论】:

    【解决方案3】:

    在使用 findViewById 之前,您必须先扩充布局/视图。

    这样:

    private Toolbar toolbar;
    
    // Set a Toolbar to replace the ActionBar.
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    cartLayout = (RelativeLayout) toolbar.findViewById(R.id.rl_cart);
    

    你的工具栏布局应该是这样的:

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right">
    
        <RelativeLayout
            android:id="@+id/rl_cart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:padding="5dp">
    
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:src="@drawable/ic_cart" />
    
        </RelativeLayout>
    </RelativeLayout>
    

    您可以在这里找到更多答案:

    How can I implement custom Action Bar with custom buttons in Android?

    How to set two custom actionbar button in left and right in android?

    【讨论】:

    • 非常感谢,它成功了。但我想知道是否有更简单的方法。
    猜你喜欢
    • 2014-07-24
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    相关资源
    最近更新 更多