自定义对话框片段中按钮的 setonclicklistener class
setonclicklistener for a button in custom dialogfragment class
我已经创建了一个自定义的 class 扩展对话框片段,当我想在主 activity 中设置我的 onclicklistener 时,我现在正在使用一个布局来膨胀到这个对话框中,但是它 returns当我设置我的点击侦听器时,空点异常是我的主要 activity 和我的对话片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button cancelDialog = (Button) findViewById(R.id.cancelDialogButton);
View.OnClickListener listenerDialog = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
//this is where i get the null point exception
cancelDialog.setOnClickListener( listenerDialog );
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ViewGroup Addition = (ViewGroup) findViewById(R.id.activity_addition);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
DialogCards editNameDialogFragment = DialogCards.newInstance();
editNameDialogFragment.show(fm, "fragment_edit_name");
}
});
现在我的服装 class 扩展对话片段:
public class DialogCards extends DialogFragment {
public Dialog dialog;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onResume() {
// Store access variables for window and blank point
Window window = getDialog().getWindow();
Point size = new Point();
// Store dimensions of the screen in `size`
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
// Set the width of the dialog proportional to 75% of the screen width
window.setLayout((int) (size.x * 0.75), WindowManager.LayoutParams.WRAP_CONTENT + 20);
window.setGravity(Gravity.CENTER);
// Call super onResume after sizing
super.onResume();
}
public DialogCards() {
}
public static DialogCards newInstance() {
DialogCards frag = new DialogCards();
Bundle args = new Bundle();
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.carddialog, container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}}
你可以在你的DialogCards中替换这个方法class
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
final Button cancelDialog = (Button)view.findViewById(R.id.cancelDialogButton);
View.OnClickListener listenerDialog = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
cancelDialog.setOnClickListener( listenerDialog );
}}
我已经创建了一个自定义的 class 扩展对话框片段,当我想在主 activity 中设置我的 onclicklistener 时,我现在正在使用一个布局来膨胀到这个对话框中,但是它 returns当我设置我的点击侦听器时,空点异常是我的主要 activity 和我的对话片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button cancelDialog = (Button) findViewById(R.id.cancelDialogButton);
View.OnClickListener listenerDialog = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
//this is where i get the null point exception
cancelDialog.setOnClickListener( listenerDialog );
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ViewGroup Addition = (ViewGroup) findViewById(R.id.activity_addition);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
DialogCards editNameDialogFragment = DialogCards.newInstance();
editNameDialogFragment.show(fm, "fragment_edit_name");
}
});
现在我的服装 class 扩展对话片段:
public class DialogCards extends DialogFragment {
public Dialog dialog;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onResume() {
// Store access variables for window and blank point
Window window = getDialog().getWindow();
Point size = new Point();
// Store dimensions of the screen in `size`
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
// Set the width of the dialog proportional to 75% of the screen width
window.setLayout((int) (size.x * 0.75), WindowManager.LayoutParams.WRAP_CONTENT + 20);
window.setGravity(Gravity.CENTER);
// Call super onResume after sizing
super.onResume();
}
public DialogCards() {
}
public static DialogCards newInstance() {
DialogCards frag = new DialogCards();
Bundle args = new Bundle();
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.carddialog, container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}}
你可以在你的DialogCards中替换这个方法class
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
final Button cancelDialog = (Button)view.findViewById(R.id.cancelDialogButton);
View.OnClickListener listenerDialog = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
cancelDialog.setOnClickListener( listenerDialog );
}}