我目前正在开发一个应用程序来将值从一个数字系统转换为另一个数字系统。我已经定义了一些代码,允许用户设置他/她的“最喜欢的”转换。
以下代码读取“conversion from”和“conversion to”的存储值,并在滑动视图中设置相应的片段。还检查了片段中相应的 RadioButton:(更新代码):
public void putFav() {
RadioButton b1,b2,b3;
String from=prefs.getString("from", "--");
String to=prefs.getString("to", "--");
if(from.contentEquals("Binary"))
{
b1=(RadioButton)findViewById(R.id.bToD);
b2=(RadioButton)findViewById(R.id.bToO);
b3=(RadioButton)findViewById(R.id.bToH);
mViewPager.setCurrentItem(1);
BinFragment ob=new BinFragment();
ob.onCreateView(getLayoutInflater(), mViewPager, mCollectionPagerAdapter.args);
if(to.contentEquals("Decimal"))
{
//Toast.makeText(getApplication(), String.valueOf(ob.DecButton.getId())+" "+String.valueOf(ob.OctButton.getId())+" "+String.valueOf(ob.HexButton.getId())+" ", Toast.LENGTH_LONG).show();
b1.setChecked(true);
b2.setChecked(false);
b3.setChecked(false);
}
if(to.contentEquals("Octal"))
{
b1.setChecked(false);
b2.setChecked(true);
b3.setChecked(false);
}
if(to.contentEquals("Hexadecimal"))
{
b1.setChecked(false);
b2.setChecked(false);
b3.setChecked(true);
}
}
else if(from.contentEquals("Decimal"))
{
b1=(RadioButton)findViewById(R.id.dToB);
b2=(RadioButton)findViewById(R.id.dToO);
b3=(RadioButton)findViewById(R.id.dToH);
mViewPager.setCurrentItem(0);
DecFragment ob=new DecFragment();
ob.onCreateView(getLayoutInflater(), mViewPager, mCollectionPagerAdapter.args);
if(to.contentEquals("Binary"))
{
b1.setChecked(true);
b2.setChecked(false);
b3.setChecked(false);
}
if(to.contentEquals("Octal"))
{
b1.setChecked(false);
b2.setChecked(true);
b3.setChecked(false);
}
if(to.contentEquals("Hexadecimal"))
{
b1.setChecked(false);
b2.setChecked(false);
b3.setChecked(true);
}
}
else if(from.contentEquals("Octal"))
{
b1=(RadioButton)findViewById(R.id.oToB);
b2=(RadioButton)findViewById(R.id.oToD);
b3=(RadioButton)findViewById(R.id.oToH);
mViewPager.setCurrentItem(2);
OctFragment ob=new OctFragment();
ob.onCreateView(getLayoutInflater(), mViewPager, mCollectionPagerAdapter.args);
if(to.contentEquals("Decimal"))
{
Toast.makeText(getApplication(), String.valueOf(ob.BinButton.getId())+" "+String.valueOf(ob.DecButton.getId())+" "+String.valueOf(ob.HexButton.getId())+" ", Toast.LENGTH_LONG).show();
/*b1.setChecked(false);
b2.setChecked(true);
b3.setChecked(false);*/
}
if(to.contentEquals("Binary"))
{
b1.setChecked(true);
b2.setChecked(false);
b3.setChecked(false);
}
if(to.contentEquals("Hexadecimal"))
{
b1.setChecked(false);
b2.setChecked(false);
b3.setChecked(true);
}
}
else if(from.contentEquals("Hexadecimal"))
{
b1=(RadioButton)findViewById(R.id.hToB);
b2=(RadioButton)findViewById(R.id.hToO);
b3=(RadioButton)findViewById(R.id.hToD);
mViewPager.setCurrentItem(3);
HexFragment ob=new HexFragment();
ob.onCreateView(getLayoutInflater(), mViewPager, mCollectionPagerAdapter.args);
if(to.contentEquals("Decimal"))
{
//Toast.makeText(getApplication(), String.valueOf(ob.BinButton.getId())+" "+String.valueOf(ob.OctButton.getId())+" "+String.valueOf(ob.DecButton.getId())+" ", Toast.LENGTH_LONG).show();
b1.setChecked(false);
b2.setChecked(false);
b3.setChecked(true);
}
if(to.contentEquals("Octal"))
{
b1.setChecked(false);
b2.setChecked(true);
b3.setChecked(false);
}
if(to.contentEquals("Binary"))
{
b1.setChecked(true);
b2.setChecked(false);
b3.setChecked(false);
}
}
else
{
Toast.makeText(this, "Favourites Not Declared. Settings->Set Favourite Conversion", Toast.LENGTH_LONG).show();
}
}
我遇到的问题是,在“二进制”和“十进制”if 语句中,RadioButtons 被正确初始化并且代码可以工作,但是在“八进制”和“十六进制”部分,即使那个代码没有初始化 RadioButtons正在执行。所有的 id 都是正确的,我已经交叉检查了它们。这是来自滑动视图的片段(八进制)之一的 layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".OctFragment"
>
<TextView
android:id="@+id/textView1"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:text="@string/oct_msg"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:textColor="#FFFFFF"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:digits="01234567."
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="45dp"
android:text="@string/convert_msg"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioButton
android:id="@+id/oToB"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="2dp"
android:text="@string/binary" />
<RadioButton
android:id="@+id/oToD"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/oToB"
android:layout_below="@+id/oToB"
android:layout_marginTop="1dp"
android:text="@string/decimal" />
<RadioButton
android:id="@+id/oToH"
android:layout_width="wrap_content"
android:textColor="#FFFFFF"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/oToD"
android:layout_below="@+id/oToD"
android:layout_marginTop="1dp"
android:text="@string/hexa" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_below="@+id/oToH"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="@string/convert" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:textColor="#FFFFFF"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_toRightOf="@+id/editText1"
android:layout_margin="5dp"
android:text="@string/equals"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:textColor="#FFFFFF"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textView5"
android:layout_margin="5dp"
android:hint="@string/null_string"
android:text="@string/null_string"
android:textAppearance="?android:attr/textAppearanceMedium" />
如果有人知道修复或导致问题的因素,请回复。:)
而且我还在学习 Java 编程,如果代码效率低下且幼稚,请见谅。提前致谢。:)