这是活动的Java文件
private TextView tv;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_agente);
tv = (TextView) findViewById(R.id.Estado);
}
//Método para el bottón rojo
protected void BotonRojo(View view){
DialogFragment dialogFragment = new DialogoOcupado();
dialogFragment.show(getSupportFragmentManager(),"Un Dialogo");
}
Thats the DialogFragment class(extends DialogFragment)
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Aviso")
.setMessage("Vas a pasar a estado Ocupado, estás seguro?")
.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}
)
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setNeutralButton("Más Tarde", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return builder.create();
}
我想在用户按下按钮“ Aceptar”时更改TextView(tv)
。我该怎么做?我正在学习编程Android,对不起。
答案 0 :(得分:1)
尝试一下
public void changeText(String text){
textView.setText(text);
}
来自对话框片段,
((Your activity)getActivity()).changeText(text);
答案 1 :(得分:1)
在您的Activity
中创建一个方法。并从Dialog
调用它。
((YourActivity)getActivity()).changeStatus("pass anything");
在您的changeStatus()
中使用Activity
方法。
public void changeStatus(String s){
tv.setText(s);
}