你好,我是android编程的初学者,这是我在这个论坛上的第一篇文章,我有一个问题。当我打开它时,应用程序崩溃了。如果你按下按钮然后出现一个对话框屏幕,如果按下确定,那么应该是一个显示ok的toast。按钮取消也是如此。按钮是onClick。
package com.example.dialogfragmenttest;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button Button1;
TextView Textview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button1 = (Button) findViewById(R.id.button1);
Textview1 = (Button) findViewById(R.id.Textview1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(){
FragmentManager manager=getFragmentManager();
FireMissilesDialogFragment schermpje = new FireMissilesDialogFragment();
schermpje.show(manager, "mydialog");
}
public void ok(){
Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
}
public void cancel(){
Toast.makeText(this, "cancel", Toast.LENGTH_LONG).show();
}
package com.example.dialogfragmenttest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.message)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// ok
MainActivity mainactivity = new MainActivity();
mainactivity.ok();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel
MainActivity mainactivity = new MainActivity();
mainactivity.cancel();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
答案 0 :(得分:0)
做,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
答案 1 :(得分:0)
试试这个: -
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
TextView title = new TextView(activity);
title.setGravity(Gravity.CENTER);
title.setText(R.string.app_name);
title.setTextSize(25);
alert.setCustomTitle(title);
alert.setMessage(msg);
alert.setCancelable(false);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO
}
});
alert.show();
答案 2 :(得分:0)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
它可以工作,因为当你想修改UI时应该使用Activity上下文。除了交叉活动,你应该使用这个'。请理解上下文。 http://androidforbeginners.blogspot.in/2009/12/context.html
答案 3 :(得分:0)
这应该有效:
MyActivity
public class MyActivity extends FragmentActivity实现了View.OnClickListener {
private FragmentManager supportFragmentManager;
private Button btn;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);//implemented onclicklistener (look up)
tv = (TextView) findViewById(R.id.Textview1);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.button1://listener for this id if view id is the same
supportFragmentManager = getSupportFragmentManager();
if(supportFragmentManager.findFragmentByTag("mydialog") != null)
{
return;
}
//dialog fragment
FireMissilesDialogFragment schermpje = new FireMissilesDialogFragment();
//arguments
Bundle bundle = new Bundle();
bundle.putString(FireMissilesDialogFragment.KEY_OK, "ok");
bundle.putString(FireMissilesDialogFragment.KEY_CANCEL, "cancel");
//set arguments
schermpje.setArguments(bundle);
//show
schermpje.show(supportFragmentManager, "mydialog");
break;
}
}
}
FireMissilesDialogFragment
public class FireMissilesDialogFragment extends DialogFragment {
public static final String KEY_OK = "key_ok";
public static final String KEY_CANCEL = "key_cancel";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String textOK = getArguments().getString(KEY_OK);
final String textCancel = getArguments().getString(KEY_CANCEL);
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(FireMissilesDialogFragment.this.getActivity(), textOK, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(FireMissilesDialogFragment.this.getActivity(), textCancel, Toast.LENGTH_SHORT).show();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}