我是android开发的初学者,被禁止进入我的项目。我整个早上都在寻找解决方案,但没有找到。
我用一个单选组和几个单选按钮进行了布局。我将它用于项目的不同视图。
我不知道如果此元素在外部视图中,则无法从视图中找到其ID。因此,我搜索了一个解决方案以将布局“包含”到我的广播组中。
没问题,我可以通过LayoutInflater获得它,并可以创建布局视图。 然后我可以得到该视图的广播组。
问题是我无法操纵它。单选组已被识别,但我无法选中单选按钮,它始终返回-1。
这是在其中创建视图的onCreate方法。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View rank_W = inflater.inflate(R.layout.dialog_layout_w, (ViewGroup) findViewById(R.id.rank_w));
View rank_M = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.rank_m));
// I also tried the commented way to make the view, it didn't change anything.
//viewRank = getLayoutInflater().inflate(R.layout.dialog_layout, null);
//viewRankW = getLayoutInflater().inflate(R.layout.dialog_layout_w, null);
tvT = findViewById(R.id.tv_T);
tvR = findViewById(R.id.tv_R);
dp = findViewById(R.id.dp);
rankM = findViewById(R.id.rankM);
rankW = findViewById(R.id.rankW);
rankM.setEnabled(false);
rankW.setEnabled(false);
gamesDB = new GamesDB(this);
rgT = findViewById(R.id.rg_t);
rgRM = rank_M.findViewById(R.id.rg_ranks);
rgRW = rank_W.findViewById(R.id.rg_ranksW);
rankM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rgT.getCheckedRadioButtonId() == R.id.rd_mm) {
rM = true;
dialogRank(view);
} else if (rgT.getCheckedRadioButtonId() == R.id.rd_wm) {
rM = true;
dialogRankW(view);
}
}
});
}
我尝试在dialogRank(view)方法中获取选中的单选按钮,这里是:
public void dialogRank(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//rgRM = rank_M.findViewById(R.id.rg_ranks);
int radioButtonID = rgRM.getCheckedRadioButtonId();
View radioButton = rgRM.findViewById(radioButtonID);
int idx = rgRM.indexOfChild(radioButton);
if (rM) {
newRankM = idx;
rM = false;
} else if (rW) {
newRankW = idx;
rW = false;
}
}
});
builder.setView(dialogLayout);
builder.show();
}
因此,在此方法中,将打开一个对话框,其中包含单选按钮的列表,而我的目标是当用户单击“确定”时获取选中的单选按钮的索引。 但是idx返回-1。
我对外部单选按钮一无所知,对此有解决方案吗?
谢谢您的阅读(对不起英语)。
答案 0 :(得分:0)
我没有弄清楚,但是我找到了另一种可行的方法。在onCreate中,我不需要获取外部布局的视图,但是方法 ID Group Ne Cars
<int> <chr> <chr> <chr>
1 1 Control A Yes
2 2 Patient A NA
3 3 Patient B No
已更改,这里是:
dialogRank(View view)
对于我在活动public void dialogRank(View view) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("Select a rank");
Button ok = dialog.findViewById(R.id.btnOK);
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
rgRM = dialog.findViewById(R.id.rg_ranks);
int idB = rgRM.getCheckedRadioButtonId();
int index = rgRM.indexOfChild(rgRM.findViewById(idB));
if (rM && index != -1) {
newRankM = index;
changeR = true;
} else if (rW && index != -1) {
newRankW = index;
changeR = true;
}
if (rM) rM = false;
if (rW) rW = false;
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
}
中声明的上下文变量。
这将打开布局final Context context = this;
的对话框,并且在cancelListner dialog_layout
中,存在此布局的单选组。方法rgRM
起作用并返回单选按钮的ID。然后,我们可以使用getCheckedRadioButtonId
获得其索引。 changeR和rM / rW变量用于我的其他治疗。
我使用rgRM.indexOfChild(rgRM.findViewById(idB))
是因为它最适合我正在使用的方法,当用户触摸对话框外的屏幕或使用后退按钮时,将使用onCancel方法。