[英]Android Radio Button Group with EditText imbedded
我的問題是我想要一個擁有3個無線電按鈕的無線電組,使用下面的方案。 三個選擇是:1。[]男2. []女3. []自定義:(自我描述身份)
但是,問題是我希望用戶在EditText中鍵入他們自己描述的身份以供我檢索。
因此,以下代碼來自我的XML頁面,其中一些元素被“####”阻止。
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="@+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="@+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button_male"
android:checked="true" />
<RadioButton android:id="@+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="@+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
如您所見,我嘗試使用LinearLayout來隔離自定義選項。 然而,存在意想不到的和不希望的副作用。 1.除了其他2個預定義性別之外,還可以選擇自定義選項。 2.無法單獨選擇自定義選項。
在活動的實際Java文件中,我有以下代碼:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
由於我對Java有點生疏,我可能會遇到一些非常新的錯誤。 請指教。
再一次,我正在尋找一種方法來獲得一組3個RadioButton,每個都在一條線上,最后一個RadioButton與EditText相鄰,從中我可以獲得所需的信息。
編輯:這是我希望它看起來像的圖片:( http://i68.tinypic.com/ao2oow.png )
不幸的是,我需要10個聲望來發布圖片。 :(
Mohit的回答使EditText與自定義輸入不同。 ( http://i63.tinypic.com/68u88x.png )
請注意,EditText的方向與自定義相鄰,而不是在下方。 我為沒有明確說明我想要的東西而道歉。
selectedId
不會是1,2或3 ....調試它你會得到價值.. 無法單獨選擇自定義選項。
LinearLayout
刪除你的第三個RadioButton
並替換下面的第二個RadioButton
並將你的EditText
和TextView
放在LinearLayout
。 在你的監聽器上獲取該RadioButton
getCheckedRadioButtonId
和getText()
並相應地檢查它...
我不知道你的任務是什么,但這里是如何讓所有三個RadioButton
工作並獲得自定義文本....
XML ...
UPDATE
<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"
tools:context="com.ex.MainActivity" >
<RadioGroup
android:id="@+id/male_female_custom_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="@+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="FeMale" />
<RadioButton
android:id="@+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Custom" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/male_female_custom_choice"
android:layout_toRightOf="@+id/male_female_custom_choice"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="@+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:ems="10"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="aa"
android:inputType="text"
android:textSize="14sp" />
<TextView
android:id="@+id/text"
android:layout_width="42dp"
android:layout_height="43dp"
android:layout_marginLeft="0dp"
android:layout_weight=".2"
android:gravity="center"
android:singleLine="true"
android:text="aaa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="@+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/male_female_custom_choice"
android:text="Get" />
</RelativeLayout>
.java文件..
public class MainActivity extends Activity {
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
public Button but;
public String str = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
but= (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = rSexGroup.getCheckedRadioButtonId();
rButton = (RadioButton)findViewById(selectedId);
if (rButton.getText().toString().equals("Male")) {
str = "Male";
}
if (rButton.getText().toString().equals("FeMale")) {
str = "FeMale";
}
if (rButton.getText().toString().equals("Custom")) {
mEdit = (EditText)findViewById(R.id.edit);
str = mEdit.getText().toString();
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
您還可以設置LinearLayout
可見性,以便僅在選中時自定義....
希望它有所幫助..
RelativeLayout
radiobutton
文本設置為空/ null EditText
添加為layout_alignParentBottom="true"
現在以編程方式調用EditText的onFocusChangeListener
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { if(b) thirdRadio.setCheched(true); } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.