我需要你的帮助。我的对话窗口有2个radiobuttons。我想在此对话框上禁用或启用某些视图,具体取决于用户检查哪个radiobutton。
我的对话框布局:inc_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/inc_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="Название дохода"
/>
<EditText
android:id="@+id/inc_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:hint="Сумма дохода"
/>
<TextView
android:id="@+id/tvChkBoxIncType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:text="Тип дохода:" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/RadioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/inc_random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:onClick="onRadioButtonClicked"
android:text="Разовый" />
<RadioButton
android:id="@+id/inc_const"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:checked="true"
android:onClick="onRadioButtonClicked"
android:text="Постоянный" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/DataPick1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="16dp"
android:text="Начало периода:"
android:enabled="false"/>
<EditText
android:id="@+id/inc_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DD"
android:enabled="false" >
</EditText>
<EditText
android:id="@+id/inc_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MM"
android:enabled="false">
</EditText>
<EditText
android:id="@+id/inc_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="YYYY" >
</EditText>
</LinearLayout>
<EditText
android:id="@+id/inc_period"
android:layout_width="wrap_content"
android:enabled="false"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:text="Каждые Х дней" >
</EditText>
</LinearLayout>
此对话框的构建:
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Добавить доход");
view = (LinearLayout) getLayoutInflater()
.inflate(R.layout.inc_dialog, null);
// устанавливаем ее, как содержимое тела диалога
adb.setView(view) .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}) .setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(),"Добавлено", Toast.LENGTH_SHORT).show();
}
}); ;
return adb.create();
}
我在ACTIVITY中调用的onClick函数
public void onRadioButtonClicked (){
switch(view.getId()) {
case R.id.inc_const:
findViewById(R.id.DataPick1).setEnabled(true);
break;
case R.id.inc_random:
findViewById(R.id.DataPick1).setEnabled(false);
break;
}
};
如果需要整个活动代码。 Income.java
package com.example.pocketbooker;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Income extends Activity{
LinearLayout view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Доходы");
setContentView(R.layout.income_const);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.inoutgo, menu);
return super.onCreateOptionsMenu(menu);
}
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Добавить доход");
view = (LinearLayout) getLayoutInflater()
.inflate(R.layout.inc_dialog, null);
// устанавливаем ее, как содержимое тела диалога
adb.setView(view) .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}) .setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(),"Добавлено", Toast.LENGTH_SHORT).show();
}
}); ;
return adb.create();
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.plus:
showDialog(1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onRadioButtonClicked (){
switch(view.getId()) {
case R.id.inc_const:
findViewById(R.id.DataPick1).setEnabled(true);
break;
case R.id.inc_random:
findViewById(R.id.DataPick1).setEnabled(false);
break;
}
};
}
所以我想动态更改对话框中视图的属性。我怎样才能做到这一点?如何从我的Activity函数调用对话框视图? 对不起我的英语,我来自哈萨克斯坦。 请帮忙。
答案 0 :(得分:1)
因为DatePicker在Dialog布局中,所以你需要在onCreateDialog方法中传递用户选择的RadioButton
id。试试看:
protected Dialog onCreateDialog(int id) {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup1);
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Добавить доход");
view = (LinearLayout) getLayoutInflater()
.inflate(R.layout.inc_dialog, null);
int radioBtnid = radioGroup.getCheckedRadioButtonId();
// get DatePicker
DatePicker datepick1=(DatePicker)view.findViewById(R.id.DataPick1);
if(radioBtnid==R.id.inc_const){
//Enable DataPick1
datepick1.setEnabled(true);
}else if(radioBtnid==R.id.inc_random){
// Disable DataPick1
datepick1.setEnabled(false);
}
//your code ...
}
答案 1 :(得分:1)
首先将View作为参数传递给onRadioButtonClicked()方法,因为XML布局中提到的onClick方法是必需的。 将视图定义为唯一参数(这将是单击的视图)
同时通过调用isChecked()方法检查传递的视图是否被选中。您的最终代码可能如下所示:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.inc_const:
if (checked)
// Do the necessary things here if inc_const is selected
findViewById(R.id.DataPick1).setEnabled(true);
break;
case R.id.inc_random:
if (checked)
// Put the code necessary if the random is checked
findViewById(R.id.DataPick1).setEnabled(false);
break;
}
}
如果有帮助,请告诉我。
答案 2 :(得分:0)
所以我做到了。
我使用了DialogFragment
为我的对话框
创建了单独的类Dialog_inc.javapackage com.example.pocketbooker;
import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
@SuppressLint("NewApi")
public class Dialog_inc extends DialogFragment implements OnClickListener {
EditText POLE;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().setTitle("Добавить Доход");
View v = inflater.inflate(R.layout.inc_dialog, null);
v.findViewById(R.id.btnYes).setOnClickListener(this);
v.findViewById(R.id.btnNo).setOnClickListener(this);
v.findViewById(R.id.inc_const).setOnClickListener(this);
v.findViewById(R.id.inc_random).setOnClickListener(this);
POLE=(EditText) v.findViewById(R.id.inc_year);
return v;
}
public void onClick(View v) {
switch(v.getId())
{case R.id.inc_const:
POLE.setEnabled(true);
break;
case R.id.inc_random:
POLE.setEnabled(false);
break;
default:
dismiss();}
}
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
}
}
为此更改了我的布局,添加了2个按钮。 inc_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/inc_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="Название дохода"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/inc_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:hint="Сумма дохода"
android:inputType="text" />
<TextView
android:id="@+id/tvChkBoxIncType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:text="Тип дохода:" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/RadioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/inc_random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:onClick="OnRadio"
android:text="Разовый" />
<RadioButton
android:id="@+id/inc_const"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:onClick="OnRadio"
android:checked="true"
android:text="Постоянный" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/DataPick1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="16dp"
android:text="Начало периода:"
/>
<EditText
android:id="@+id/inc_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DD" >
</EditText><EditText
android:id="@+id/inc_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MM"
>
</EditText><EditText
android:id="@+id/inc_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YYYY" >
</EditText>
</LinearLayout>
<EditText
android:id="@+id/inc_period"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:onClick="onRadioButtonClicked"
android:text="Каждые Х дней" >
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="yes">
</Button>
<Button
android:id="@+id/btnNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="no">
</Button>
</LinearLayout>
</LinearLayout>
我把这个对话称为这样。 Income.class
package com.example.pocketbooker;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
public class Income extends Activity{
LinearLayout view;
DialogFragment dlg1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Доходы");
setContentView(R.layout.income_const);
dlg1 = new Dialog_inc();
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.inoutgo, menu);
return super.onCreateOptionsMenu(menu);
}
@SuppressLint("NewApi")
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.plus:
dlg1.show(getFragmentManager(), "dlg1");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
当然,现在它不是一个漂亮的代码。但我希望它可以帮助别人。谢谢大家回复