我是这里的新手和java。我正在开发一个带有单选按钮的Android应用程序,用于计算学生的SGPA。每个科目都拥有自己的学分,这些学分会被相应的年级倍增,并且在加入主题的所有产品后,总和除以总学分...
到目前为止,我已经正确地完成了xml代码,但我仍然坚持使用java主要活动......我只需要一个按钮或主题的示例,我只需要自己做...我想要使用开关盒首先检查单击哪个单选按钮,然后进行相应的计算。
XML CODE
<TextView
android:id="@+id/maths"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F44336"
android:text="MATHEMATICS"
android:textAlignment="center"
android:textAllCaps="true"
android:textAppearance="?android:textAppearanceLarge"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F44336"
android:orientation="horizontal">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="horizontal">
<RadioButton
android:id="@+id/Omaths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="O"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/Aplusmaths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A+"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/Amaths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/Bplusmaths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B+"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/Bmaths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/Cmaths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/Pmaths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="P"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
</LinearLayout>
和JAVA主要活动代码
package droidmentor.bnv_with_viewpager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import droidmentor.bnv_with_viewpager.Fragment.CallsFragment;
import droidmentor.bnv_with_viewpager.Fragment.ChatFragment;
import droidmentor.bnv_with_viewpager.Fragment.ContactsFragment;
import static droidmentor.bnv_with_viewpager.R.id.Amaths;
import static droidmentor.bnv_with_viewpager.R.id.Bmaths;
import static droidmentor.bnv_with_viewpager.R.id.Bplusmaths;
import static droidmentor.bnv_with_viewpager.R.id.Cmaths;
import static droidmentor.bnv_with_viewpager.R.id.Pmaths;
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
//This is our viewPager
private ViewPager viewPager;
//Fragments
ChatFragment chatFragment;
CallsFragment callsFragment;
ContactsFragment contactsFragment;
MenuItem prevMenuItem;
//Instance Variables For Each Subject
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.viewpager);
//Initializing the bottomNavigationView
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_call:
viewPager.setCurrentItem(0);
break;
case R.id.action_chat:
viewPager.setCurrentItem(1);
break;
case R.id.action_contact:
viewPager.setCurrentItem(2);
break;
}
return false;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (prevMenuItem != null) {
prevMenuItem.setChecked(false);
}
else
{
bottomNavigationView.getMenu().getItem(0).setChecked(false);
}
Log.d("page", "onPageSelected: "+position);
bottomNavigationView.getMenu().getItem(position).setChecked(true);
prevMenuItem = bottomNavigationView.getMenu().getItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
/* //Disable ViewPager Swipe
viewPager.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
*/
setupViewPager(viewPager);
}
//This method is called when button is pressed
public void calculate(View view) {
display(9);
}
//This method returns the SGPA
private void display(int number) {
TextView sgpa = (TextView) findViewById(R.id.sgpa);
sgpa.setText("" + number);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
callsFragment=new CallsFragment();
chatFragment=new ChatFragment();
contactsFragment=new ContactsFragment();
adapter.addFragment(callsFragment);
adapter.addFragment(chatFragment);
adapter.addFragment(contactsFragment);
viewPager.setAdapter(adapter);
}
}
我知道我可以使用带编辑文本的用户输入法,但我必须使用单选按钮... :)
答案 0 :(得分:0)
尝试执行此操作以获取选中按钮:
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
请注意,如果您在radioGroup中使用 textview 或其他视图不等于 radiobutton ,则会得到错误的索引。
获取所选按钮的文本:
RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
String selectedtext = r.getText().toString();