我有Fragment
,显示Dialog
。
在Dialog
内,有一个RadioGroup
。
我试图为这个RadioGroup
创建一个听众,但我没有成功。
Fragment
- > Dialog
- > RadioGroup
我尝试了四种方式:
OnCheckedChangeListener
上RadioGroup
。 OnClickListener
RadioGroup
。
{li> OnCheckedChangeListener
RadioButton
。
OnClickListener
RadioButton
没有人工作。
这是我的代码:
Dialog
- 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:id="@+id/bS_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RadioButton
android:id="@+id/r_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="ID"
android:textSize="24sp" />
<RadioButton
android:id="@+id/r_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Title"
android:textSize="24sp" />
<RadioButton
android:id="@+id/r_genre"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Genre"
android:textSize="24sp" />
<RadioButton
android:id="@+id/r_author"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Author"
android:textSize="24sp" />
</RadioGroup>
<AutoCompleteTextView
android:id="@+id/c_bSearch"
android:layout_width="278dp"
android:layout_height="72dp"
android:singleLine="true"/>
</LinearLayout>
Fragment
- 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.codecrafters.tableview.TableView
android:id="@+id/tableView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
和Fragment
- 代码:(我标记的问题在哪里,其他代码并不重要)
package com.example.adiel.library_app_30;
import .....;
public class ShowAllBooks extends Fragment {
View v;
TableLayout table;
TableRow row;
String[] spaceProbeHeaders = {"ID", "Title"};
String[][] spaceProbes;
AutoCompleteTextView c_search;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.show_all_books, container, false);
setHasOptionsMenu(true);
/*
------The problem is here----
*/
LayoutInflater li = LayoutInflater.from(v.getContext());
View promptsView = li.inflate(R.layout.b_search_dialog, null);
c_search = (AutoCompleteTextView) promptsView.findViewById(R.id.c_bSearch);
RadioGroup rg = (RadioGroup) promptsView.findViewById(R.id.bS_rg);
rg.check(R.id.r_id);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
Log.d("Log", "Clicked");
}
});
rg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Log", "Clicked");
}
});
RadioButton r_id = (RadioButton) promptsView.findViewById(R.id.r_id);
r_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("Log", "Clicked");
}
});
r_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Log", "Clicked");
}
});
/*
------Until here----
*/
final TableView<String[]> tb = (TableView<String[]>) v.findViewById(R.id.tableView);
tb.setColumnCount(2);
tb.setHeaderBackgroundColor(Color.parseColor("#2ecc71"));
Ask a = new Ask("books", "getAllCopiesOfBooks", null);
Client client = (Client) new Client(a, a.getIp(), v.getContext(), new Client.ClientReply() {
@Override
public void processFinish(Object output) {
if (output != null) {
ArrayList<CopyId> data = (ArrayList<CopyId>) output;
spaceProbes = new String[data.size()][2];
for (int i = 0; i < data.size(); i++) {
CopyId c = data.get(i);
spaceProbes[i][0] = String.valueOf(c.getId());
spaceProbes[i][1] = c.getTitle();
}
tb.setHeaderAdapter(new SimpleTableHeaderAdapter(v.getContext(), spaceProbeHeaders));
tb.setDataAdapter(new SimpleTableDataAdapter(v.getContext(), spaceProbes));
}
}
}).execute();
tb.addDataClickListener(new TableDataClickListener() {
@Override
public void onDataClicked(int rowIndex, Object clickedData) {
Toast.makeText(v.getContext(), ((String[]) clickedData)[0], Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), ShowBook.class);
i.putExtra("id", ((String[]) clickedData)[0]);
startActivity(i);
}
});
return v;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.showallbooks_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_add:
return true;
case R.id.action_search:
search_func();
return true;
}
return super.onOptionsItemSelected(item);
}
public void search_func(){
final Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.b_search_dialog);
dialog.setTitle("Search");
dialog.show();
//RadioButton r_id = (RadioButton) promptsView.findViewById(R.id.r_id);
/*RadioButton r_title = (RadioButton) v.findViewById(R.id.r_title);
RadioButton r_genre = (RadioButton) v.findViewById(R.id.r_genre);
RadioButton r_author = (RadioButton) v.findViewById(R.id.r_author);*/
}
}