0
以下是我正在嘗試執行的操作: 我有2個活動,一個MainActivity和一個SettingsActivity,它通過單擊MA中的按鈕打開。在SA中,有6個單選按鈕,每個按鈕在收音機組中都有相應的顏色ID(如if/else語句中所示)以及用於關閉該活動的按鈕。當用戶按下關閉按鈕時,MA的背景將變爲用戶選擇的顏色。除了getCheckedRadioButtonId()方法獲取創建SettingsActivity時選擇的按鈕的ID(無)之外,一切運行良好,因此顯示的顏色總是最後一個else塊中的顏色。我知道這應該是一個非常簡單的修復,但我花了大約4個小時嘗試我能想到的每個解決方案,並且我的許多谷歌搜索都沒有得到任何有用的東西。以下是相關代碼:單擊按鈕時獲取所選單選按鈕的編號
SettingsActivity.java:
public class SettingsActivity extends AppCompatActivity {
public static final String colorID = "colorID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
LayoutInflater inflater = LayoutInflater.from(SettingsActivity.this);
View settingsView= inflater.inflate(R.layout.activity_settings, null);
returnToMain();
}
public int getColorValueID(View view) {
final int colorId;
int id;
final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
if(radioGroup.getCheckedRadioButtonId()!= -1) {
id = radioGroup.getCheckedRadioButtonId();
}
else {
id = 0;
}
if (id == R.id.redButton) {
colorId = R.color.red_theme;
} else if (id == R.id.blueButton) {
colorId = R.color.blue_theme;
} else if (id == R.id.greenButton) {
colorId = R.color.green_theme;
} else if (id == R.id.orangeButton) {
colorId = R.color.orange_theme;
} else if (id == R.id.purpleButton) {
colorId = R.color.purple_theme;
} else {
colorId = R.color.blue_theme;
}
return colorId;
}
public void returnToMain(){
Button returnButton = (Button) findViewById(R.id.backToMain);
returnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = LayoutInflater.from(SettingsActivity.this);
View settingsView= inflater.inflate(R.layout.activity_settings, null);
int color = getColorValueID(settingsView);
final Intent mIntent = new Intent();
mIntent.putExtra(colorID, color);
setResult(RESULT_OK, mIntent);
finish();
}
});
}
}
activity_settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rileybolen.simplemortgagecalculator.SettingsActivity"
tools:background="#dedede">
<TextView
android:text="@string/settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/settingsHeading"
android:textColor="#494949"
android:textSize="32sp"
android:textAlignment="center"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/themeHeading"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:id="@+id/radioGroup">
<RadioButton
android:text="Orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/orangeButton"
android:layout_alignParentTop="true"
android:textColor="@color/orange_theme"
android:textSize="18sp"
android:layout_marginLeft="20dp"
/>
<RadioButton
android:text="Purple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/purpleButton"
android:layout_below="@id/orangeButton"
android:textColor="@color/purple_theme"
android:textSize="18sp"
android:layout_marginLeft="20dp"/>
<RadioButton
android:text="Grey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/greyButton"
android:layout_below="@id/purpleButton"
android:textColor="#494949"
android:textSize="18sp"
android:layout_marginLeft="20dp"/>
<RadioButton
android:text="Red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/redButton"
android:layout_alignParentTop="true"
android:textColor="@color/red_theme"
android:textSize="18sp"
android:layout_marginLeft="20dp"/>
<RadioButton
android:text="Blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/blueButton"
android:layout_below="@id/redButton"
android:textColor="@color/blue_theme"
android:textSize="18sp"
android:layout_marginLeft="20dp"/>
<RadioButton
android:text="Green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/greenButton"
android:layout_below="@id/blueButton"
android:textColor="@color/green_theme"
android:textSize="18sp"
android:layout_marginLeft="20dp"/>
</RadioGroup>
<TextView
android:text="@string/theme_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:id="@+id/themeHeading"
android:textColor="#494949"
android:textSize="24sp"
android:layout_below="@+id/settingsHeading"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:text="Close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/backToMain"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
我試着用其中的一個,但它沒有爲我工作,因爲ID必須提供給returnToMain方法,當我分配checkedId int變量也只是內部可用監聽器。我也試過在監聽器裏調用returnToMain並傳入checkedId,但那也不起作用。 –
在監聽器中放置烤麪包,並切換單選按鈕以確保其觸發。另外,在你的聲明中,在類中聲明一個int作爲字段變量,並將偵聽器中的checkedID分配給該變量 – BiGGZ
創建一個int字段變量起作用!我在onCreate上面創建了private int mColor,然後將它設置爲onCreate中的偵聽器中的checkedId,然後將getColorValueId設置爲int並在mColor中傳入,當我在returnToMain中調用它時。謝謝! –