Java 活动代码放在哪里?
因此,我有一个检查某些内容的代码,并将其放在活动的Java 活动代码放在哪里?,java,android,android-activity,Java,Android,Android Activity,因此,我有一个检查某些内容的代码,并将其放在活动的onCreate()中。我想知道放在那里是否正确,而且,出于某种原因,检查主要活动的代码根本不起作用,第二个有toast的代码起作用。我认为问题可能出在一个对话中。这是一个烤面包片: AlertDialog.Builder Dial = new AlertDialog.Builder(Screen.this); Dial.setTitle(R.string.Dial_Tit); Dial.setMessage(R.string.Dial_Mes)
onCreate()
中。我想知道放在那里是否正确,而且,出于某种原因,检查主要活动的代码根本不起作用,第二个有toast的代码起作用。我认为问题可能出在一个对话中。这是一个烤面包片:
AlertDialog.Builder Dial = new AlertDialog.Builder(Screen.this);
Dial.setTitle(R.string.Dial_Tit);
Dial.setMessage(R.string.Dial_Mes);
Dial.setPositiveButton("OK", PosBC());
Dial.setNegativeButton(R.string.Dial_NegBC, NegBC());
Dial.show();
注意:两个按钮都有方法,我只是没有发布它们。问题是警报甚至没有显示。出于某种原因,toast确实有效,它就像自动单击按钮,甚至认为该方法有一个不起作用的意图
根据要求提供更多代码:
private DialogInterface.OnClickListener NegBC() {
Intent moveToStart;
moveToStart = new Intent(Screen.this, Launch.class);
startActivity(moveToStart);
return null;
}
private DialogInterface.OnClickListener PosBC() {
startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
Toast.makeText(getApplicationContext(), R.string.settingsToast, Toast.LENGTH_LONG).show();
return null;
}
更新:我添加了显示对话框的
create()
方法,但它是这样的:创建活动时显示toast,按back返回设置,从设置显示对话框按back,按钮不起作用。在OnCreateDialog中编写AlertDialog代码,并在OnCreate中启动一个用于检查的对话框,一旦任务完成,在onPostExecute中关闭对话框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDialog(0x01);
}
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder Dial;
switch (id) {
case 0x01:
Dial = new AlertDialog.Builder(Screen.this);
Dial.setTitle(R.string.Dial_Tit);
Dial.setMessage(R.string.Dial_Mes);
Dial.setPositiveButton("OK", PosBC());
Dial.setNegativeButton(R.string.Dial_NegBC, NegBC());
Dial.create();
break;
default:
break;
}
return super.onCreateDialog(id);
}
无论何时启动应用程序,如果未缓存在设备的RAM中,都将调用onCreate()
除此之外,我不明白您想要实现什么,请通过添加更多代码来编辑您的帖子,我还将编辑我的答案以获得更详细的信息。使用此代码在android中单击按钮显示alertDialog:
package .....; // name of your package.
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AlertDialogActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAlertTwoBtns = (Button) findViewById(R.id.button1);
btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Creating alert Dialog with two Buttons
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle(" "); //type your title here insid the quotes.
// Setting Dialog Message
alertDialog.setMessage(" "); //type the message which is to be displayed
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.ic_launcher); // set the icon from drawable folder just put the icon file in drawable folder.
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); // just a sample code to tell that what things you can do here
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
});
}
}
好吧,我自己解决了,结果只是逻辑缺失:D.对不起 你能详细说明一下吗,我是个傻瓜,不太懂。斯莱,难道没有更简单的方式来显示对话吗?我的意思是我想要一些我能理解的东西。应用程序不应该阻止UI线程,这就是为什么检查操作应该在不同的线程中进行。我仍然不明白,创建对话框有多难?我不需要在单击按钮时使用它,它必须在if语句中执行。然后删除onclick函数和按钮,并添加类似if(something){然后键入警报对话框代码}