Java 我在Android Studio中刷入微粒类后如何显示自动警报对话框?
我想显示对话框/弹出窗口,显示给用户的消息,显示请用一个输入按钮输入您的姓名。点击按钮进入数据库显示信息。如果输入的用户名错误,则应弹出错误消息。 有人能帮我吗?我想在刷卡进入微粒类时自动显示对话框/弹出窗口 这里的主要问题是我已经将我的类扩展到了Java 我在Android Studio中刷入微粒类后如何显示自动警报对话框?,java,android,Java,Android,我想显示对话框/弹出窗口,显示给用户的消息,显示请用一个输入按钮输入您的姓名。点击按钮进入数据库显示信息。如果输入的用户名错误,则应弹出错误消息。 有人能帮我吗?我想在刷卡进入微粒类时自动显示对话框/弹出窗口 这里的主要问题是我已经将我的类扩展到了Fragment,现在对于Dialog,我应该扩展另一个类DialogFragment,但在一个类中,我只能扩展一个类。 请帮帮我 主要活动 { Credit.java代码 package com.example.jawa.pops; import
Fragment
,现在对于Dialog,我应该扩展另一个类DialogFragment
,但在一个类中,我只能扩展一个类。
请帮帮我
主要活动
{
Credit.java代码
package com.example.jawa.pops;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by jawa on 10/4/2015.
*/
public class Credit extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.credit, container, false);
}
}
现在,当我打开credit.java时,我想自动显示一个对话框
package com.example.jawa.pops;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Credit extends Fragment implements CreditDialog.Callback{
MainActivity mActivity;
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
mActivity = (MainActivity)activity;
}
catch(ClassCastException e)
{
Log.e("Credit", "ClassCastException e", e);
}
}
@Override
public void onStart()
{
super.onStart();
if(mActivity !=null){
CreditDialog dialog = new CreditDialog();
dialog.setTargetFragment(this, 1);
mActivity.showDialog(dialog);
}
}
@Override
public void onEnterPressed(String name)
{
//manage answer
}
}
下面是CreditDialog类:
public class CreditDialog extends DialogFragment implements View.OnClickListener
{
private EditText mEditText;
private Button enterButton;
public static interface Callback
{
public void onEnterPressed(String name);
}
public CreditDialog()
{
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.dialogfragment, container);
mEditText =(EditText) view.findViewById(R.id.name);
enterButton = (Button) view.findViewById(R.id.enterButton);
enterButton.setOnClickListener(this);
getDialog().setTitle(R.string.dialog_title);
return view;
}
@Override
public void onClick(View v)
{
Callback callback = null;
try
{
callback = (Callback) getTargetFragment();
}
catch (ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "Callback of this class must be implemented by target fragment!", e);
throw e;
}
if (callback != null)
{
String name="";
if(mEditText !=null) {
name=mEditText.getText().toString();
}
callback.onEnterPressed(name);
this.dismiss();
}
}
}
请发布您迄今为止尝试过的代码。只需在
main活动的onResume
中打开AlertDialog
,因为Credit
是滑块的第一个视图
public class CreditDialog extends DialogFragment implements View.OnClickListener
{
private EditText mEditText;
private Button enterButton;
public static interface Callback
{
public void onEnterPressed(String name);
}
public CreditDialog()
{
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.dialogfragment, container);
mEditText =(EditText) view.findViewById(R.id.name);
enterButton = (Button) view.findViewById(R.id.enterButton);
enterButton.setOnClickListener(this);
getDialog().setTitle(R.string.dialog_title);
return view;
}
@Override
public void onClick(View v)
{
Callback callback = null;
try
{
callback = (Callback) getTargetFragment();
}
catch (ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "Callback of this class must be implemented by target fragment!", e);
throw e;
}
if (callback != null)
{
String name="";
if(mEditText !=null) {
name=mEditText.getText().toString();
}
callback.onEnterPressed(name);
this.dismiss();
}
}
}