在本文中,我们将详细介绍android-在同一活动中显示2个警报对话框的各个方面,同时,我们也将为您带来关于Android::同一活动中的多个按钮、android–Appcompat警报对话框操作按钮
在本文中,我们将详细介绍android-在同一活动中显示2个警报对话框的各个方面,同时,我们也将为您带来关于Android :: 同一活动中的多个按钮、android – Appcompat警报对话框操作按钮背景按下状态、android – 使用RecyclerView的自定义警报对话框、android – 使用自定义Anko布局DSL解除警报对话框的有用知识。
本文目录一览:- android-在同一活动中显示2个警报对话框
- Android :: 同一活动中的多个按钮
- android – Appcompat警报对话框操作按钮背景按下状态
- android – 使用RecyclerView的自定义警报对话框
- android – 使用自定义Anko布局DSL解除警报对话框
android-在同一活动中显示2个警报对话框
嗨,我尝试设置2个警报对话框,一个接一个,但是它崩溃了,我不知道如何解决,显示了第一个,但是当我单击“确定”时,它停止了(它没有崩溃并显示红色错误但它停止工作)
如果有人可以找出问题出在哪里,它将为我节省代码
AlertDialog dialogo = new AlertDialog.Builder(this)
.setTitle("Introduce un nombre")
.setMessage("Esta es la primera vez que subes una foto, introduce un nombre que aparecera en tu foto")
.setView(input)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String nombre = String.valueOf(input.getText());
if(nombre.equalsIgnoreCase(""))
{
Toast.makeText(ctx,"¡Tu nombre no puede estar en blanco!", Toast.LENGTH_LONG).show();
//entra =1;
}
else
{
editor.putString("nombre",nombre);
editor.commit();
//entra = 0;
AlertDialog dialogo3 = new AlertDialog.Builder(ctx)
.setTitle("Introduce un nombre")
.setMessage("Esta es la primera vez que subes una foto, introduce un nombre que aparecera en tu foto")
.setView(input)
.setPositiveButton("Hombre", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
editor.putString("genero","Hombre");
editor.commit();
GestorSubirFotos gestor = new GestorSubirFotos(ctx,prefs);
String elnombre = prefs.getString("nombre", "");
gestor.execute(getRealPathFromURI(selectedImage),id,elnombre,phoneid);
}
}).setNegativeButton("Mujer", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
editor.putString("genero","Mujer");
editor.commit();
GestorSubirFotos gestor = new GestorSubirFotos(ctx,prefs);
String elnombre = prefs.getString("nombre", "");
gestor.execute(getRealPathFromURI(selectedImage),id,elnombre,phoneid);
}
}).show();
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//entra =1;
}
}).show();
我也尝试过这种方法,但它也不起作用
final Builder dialogo3 = new AlertDialog.Builder(ctx)
.setTitle("Introduce un nombre")
.setMessage("Esta es la primera vez que subes una foto, introduce un nombre que aparecera en tu foto")
.setView(input)
.setPositiveButton("Hombre", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
editor.putString("genero","Hombre");
editor.commit();
GestorSubirFotos gestor = new GestorSubirFotos(ctx,prefs);
String elnombre = prefs.getString("nombre", "");
gestor.execute(getRealPathFromURI(selectedImage),id,elnombre,phoneid);
}
}).setNegativeButton("Mujer", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
editor.putString("genero","Mujer");
editor.commit();
GestorSubirFotos gestor = new GestorSubirFotos(ctx,prefs);
String elnombre = prefs.getString("nombre", "");
gestor.execute(getRealPathFromURI(selectedImage),id,elnombre,phoneid);
}
});
Builder dialogo = new AlertDialog.Builder(this)
.setTitle("Introduce un nombre")
.setMessage("Esta es la primera vez que subes una foto, introduce un nombre que aparecera en tu foto")
.setView(input)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String nombre = String.valueOf(input.getText());
if(nombre.equalsIgnoreCase(""))
{
Toast.makeText(ctx,"¡Tu nombre no puede estar en blanco!", Toast.LENGTH_LONG).show();
//entra =1;
}
else
{
editor.putString("nombre",nombre);
editor.commit();
//entra = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
dialogo3.show();
}
}, 2000);
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//entra =1;
}
});
dialogo.show();
解决方法:
试试这个,现在应该可以工作了->
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do something
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Do you really want to unjoin this event?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", null)
.show();
Android :: 同一活动中的多个按钮
如何解决Android :: 同一活动中的多个按钮?
这是我的问题:
我想在一个活动中实现两个按钮。一个是计算BMI,另一个是去查时间。我希望用户可以在同一个活动中看到两个按钮,这也取决于他们想要做什么。 (这些功能不是同时工作的,是分开工作的。另外,我在这两个活动中都实现了返回主页的按钮。)
在 Android 模拟器中,消息“应用程序继续停止。”
以下是 MainActivity.java 文件:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// bt1 = to BMI btn = TO DateCheck
Button bt1 = findViewById(R.id.button1);
Button btn = findViewById(R.id.button);
bt1.setonClickListener(v -> {
// 指定要呼叫的 Activity Class
Intent newAct = new Intent();
newAct.setClass(MainActivity.this,BMI.class);
// 呼叫新的 Activity Class
startActivity(newAct);
});
btn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent newAct2 = new Intent();
newAct2.setClass(MainActivity.this,Time_Date.class);
startActivity(newAct2);
}
});
}
}
Following is the **Manifest.xml file**:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Time_Date"/>
<activity android:name=".BMI" />
</application>
</manifest>
Following is **activity_main.xml**:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#87CEEA">
<!-- to BMI -->
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="100dp"
android:layout_marginRight="100dp"
android:backgroundTint="@color/pink"
android:text="@string/to_bmi"
android:textAlignment="center"
android:textColor="@color/purple_500"
android:textandroid:textSize="16sp"/>
<!--to DateCheck-->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="100dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="100dp"
android:text="@string/view_datetime"
android:textandroid:textSize="16sp"
android:textColor="@color/purple_500"
android:backgroundTint="@color/pink"
/>
</RelativeLayout>
谢谢!
这些消息是我按下导致应用停止的按钮后的堆栈跟踪:
com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream
at com.google.android.apps.gsa.staticplugins.recognizer.j.a.a(SourceFile:28)
at com.google.android.apps.gsa.staticplugins.recognizer.j.b.run(SourceFile:15)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(UnkNown Source:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6)
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow,no available space.
at com.google.android.apps.gsa.speech.audio.Tee.f(SourceFile:103)
at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)
at java.io.InputStream.read(InputStream.java:101)
at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:18)
at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(UnkNown Source:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6)
解决方法
对于意图使用此代码:
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
,
看看this的答案。
该错误与您的 2 个按钮无关,而是与您刚刚启动的活动中发生的某些事情有关的模拟器错误。
我想在这里注意的一些事情:
您将开始和左侧以及右侧和结束混合在一起。你应该决定其中之一。差异解释here。
您可以按照 mohammadjavads sabagh''s answer
所述缩短意图的代码使用 CTRL/CMD + ALT + L 格式化你的代码(也适用于 xml)?
android – Appcompat警报对话框操作按钮背景按下状态
它在图像中的效果非常好(在所有Android版本中).
AlertDialog的样式就是这个. (目前我正在使用硬编码颜色值而不是颜色资源)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimaryDark">#111111</item> <item name="colorPrimary">#00ddff</item> <item name="colorAccent">#0044aa</item> <item name="colorButtonnormal">#00aaaa</item> <item name="colorControlHighlight">#00ddff</item> <item name="alertDialogTheme">@style/AlertDialogTheme</item> </style> <style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert"> <item name="colorAccent">#0044aa</item> <item name="android:background">#ffffff</item> <item name="android:textColorPrimary">#000000</item> <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item> </style> <style name="MyTitleTextStyle"> <item name="android:textColor">#0044aa</item> <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item> </style>
我的问题是,
1)如何更改图像中圆角(灰色)的statepressed颜色?
2)android> = 21中没有按下的颜色,这是什么黑客攻击?
3)如何使用不同颜色的动作按钮(是否可能)?
任何帮助都会很棒.
解决方法
> buttonbarButtonStyle
> buttonbarNegativeButtonStyle
> buttonbarNeutralButtonStyle
> buttonbarPositiveButtonStyle
例:
<style name="dialog_theme" parent="Theme.AppCompat.Dialog.Alert"> <item name="buttonbarNegativeButtonStyle">@style/dialog_button.negative</item> <item name="buttonbarPositiveButtonStyle">@style/dialog_button.positive</item> </style> <style name="dialog_button"> <item name="android:textStyle">bold</item> <item name="android:minWidth">64dp</item> <item name="android:paddingLeft">8dp</item> <item name="android:paddingRight">8dp</item> <item name="android:background">@drawable/dialogButtonSelector</item> </style> <style name="dialog_button.negative"> <item name="android:textColor">#f00</item> </style> <style name="dialog_button.positive"> <item name="android:layout_marginLeft">8dp</item> <item name="android:textColor">#00f</item> </style>
其中dialogButtonSelector是我们的自定义drawable选择器.
不幸的是在dialog_button上设置背景会破坏我们的填充和边距,所以我需要再次设置它.
dialog_button样式可以通过Widget.AppCompat.Button.buttonbar.AlertDialog继承,但我发现它缺少像textStyle粗体样式.
android – 使用RecyclerView的自定义警报对话框
我正在使用RecyclerView列出一些文本,现在我想这样做,以便当用户点击文本时弹出一个自定义警报对话框.
到目前为止我已尝试过这个但是得到一个NullPointerException;这可能有什么问题?
public class CBAdapter extends RecyclerView.Adapter<CBAdapter.ViewHolder> {
List<AdapterData> mItems;
public CBAdapter() {
super();
mItems = new ArrayList<>();
AdapterData data = new AdapterData();
data.setTextOne("Many Bows");
mItems.add(data);
data = new AdapterData();
data.setTextOne("Pardon");
mItems.add(data);
data = new AdapterData();
data.setTextOne("Fall To Knees & Beg");
mItems.add(data);
data = new AdapterData();
data.setTextOne("Backflips");
mItems.add(data);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.test3, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
AdapterData data = mItems.get(i);
viewHolder.textOne.setText(data.getTextOne());
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView textOne;
private Context context;
public ViewHolder(View itemView) {
super(itemView);
textOne = (TextView)itemView.findViewById(R.id.textView1);
itemView.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Title");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("hello world");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setimageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
}
解决方法:
没关系我忘了初始化我的上下文
context = itemView.getContext();
android – 使用自定义Anko布局DSL解除警报对话框
我创建了以下警告对话框,其中包含一个包含TextView,EditText和Button的简单视图:
alert {
customView {
verticalLayout {
textView {
text = getString(R.string.enter_quantity)
textSize = 18f
textColor = Color.BLACK
}.lparams {
topMargin = dip(17)
horizontalMargin = dip(17)
bottomMargin = dip(10)
}
val quantity = editText {
inputType = InputType.TYPE_CLASS_NUMBER
background = ContextCompat.getDrawable(this@ProductsList, R.drawable.textBox_bg)
}.lparams(width = matchParent, height = wrapContent) {
bottomMargin = dip(10)
horizontalMargin = dip(17)
}
button(getString(R.string.confirm)) {
background = ContextCompat.getDrawable(this@ProductsList, R.color.colorPrimary)
textColor = Color.WHITE
}.lparams(width = matchParent, height = matchParent) {
topMargin = dip(10)
}.setonClickListener {
if (quantity.text.isNullOrBlank())
snackbar(parentLayout!!, getString(R.string.enter_valid_quantity))
else
addToCart(product, quantity.text.toString().toInt())
}
}
}
}.show()
我想在单击按钮并执行if-else子句时忽略它.我尝试使用这个@ alert但它没有提供对话框方法.
解决方法:
这是有问题的,因为当对话框尚不存在时,您的按钮函数调用会注册侦听器.
这是一种方法,使用本地lateinit
变量使对话框在侦听器中可用:
lateinit var dialog: DialogInterface
dialog = alert {
customView {
button("Click") {
dialog.dismiss()
}
}
}.show()
您还可以将构建器的结果分配给类属性等.请注意,局部变量的lateinit可用于since Kotlin 1.2.
关于android-在同一活动中显示2个警报对话框的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android :: 同一活动中的多个按钮、android – Appcompat警报对话框操作按钮背景按下状态、android – 使用RecyclerView的自定义警报对话框、android – 使用自定义Anko布局DSL解除警报对话框等相关内容,可以在本站寻找。
本文标签: