[英]How to send the value of chosen RadioButton and DatePicker to second Activity?
[英]Checking a radiobutton if a specific activity is chosen
在我的代碼中,當活動被打開/開始時,我試圖檢查菜單中的單選按鈕之一。 我的onCreate方法中有一個函數,但出現錯誤,提示“無法取消引用int”。 在這種情況下如何解決? 或者,是否有完全不同的方法可以實現我的目標?
onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_markets);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
R.id.servemenu.setChecked(true); //this what what I am using to check the radio button
//home button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ActivityMarkets.this, ActivityMenu.class);
startActivity(intent);
}
});
//Don't know what this is for but I'm not gonna mess with it
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
//Menu I think
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
.xml菜單代碼
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/servemenu"
android:title="Markets We Serve" />
<item
android:id="@+id/federalmenu"
android:title="Federal" />
<item
android:id="@+id/statelocalmenu"
android:title="State and Local" />
<item
android:id="@+id/commercialmenu"
android:title="Commercial" />
</group>
您不能直接使用該按鈕來引用它。
R.id.servemenu
只是一個整數,用於標識視圖中的R.id.servemenu
按鈕。 如果確實是RadioButton的ID,則必須使用R.id.servemenu
檢索R.id.servemenu
按鈕。
你應該這樣
RadioButton rb = (RadioButton) findViewById(R.id.servemenu); rb.setChecked(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.