简体   繁体   English

对话框标题未出现在Android中

[英]Dialog Title Does not appear in Android

I'm trying to create a dialog. 我正在尝试创建一个对话框。 All of the items appear and it responds to touch, but the title doesn't appear. 出现所有项目,并且它对触摸有反应,但是标题没有出现。 I checked other answers and they suggested setting "windowNoTitle" to "false" in the xml but this hasn't helped. 我检查了其他答案,他们建议在xml中将“ windowNoTitle”设置为“ false”,但这无济于事。 Nor has trying "setTitle" after building the dialog (which is how it is written now). 建立对话框后也没有尝试“ setTitle”(现在是这样写的)。 Why does my title not appear? 为什么我的标题没有出现?

Here is the code for MainActivity: 这是MainActivity的代码:

package samapps.myfirstapp;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity implements DialogTest.OnFragmentInteractionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onButtonClick(int result){
        Log.d("tag","Executed on click");
    }

    //Called when the Send button is tapped
    public void sendMessage(View view){
       String[] text = {"Test 1","Test 2","Test 3"};
        String title = "Test Title";
        DialogFragment fragment = new DialogTest();
        Bundle args = new Bundle();
        args.putString("title_question",title);
        args.putStringArray("choices",text);
        fragment.setArguments(args);
        fragment.show(getSupportFragmentManager(),"testtest123");
        Log.d("tag","Executed after dialog");
}

} }

And the dialog fragment: 和对话框片段:

package samapps.myfirstapp;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;



public class DialogTest extends DialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public DialogTest() {
        // Required empty public constructor
    }

    public interface OnFragmentInteractionListener {
        public void onButtonClick(int result);
    }



    @Override
    public Dialog onCreateDialog (Bundle savedInstanceState){
        Bundle theBundle = this.getArguments();

        String[] choices = theBundle.getStringArray("choices");
        String choiceTitle = theBundle.getString("choice_title");

        AlertDialog.Builder builder;
        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Material_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(getActivity().getParent());
        }
        builder.setTitle(choiceTitle);

        builder.setItems(choices,new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog,int which){
                mListener.onButtonClick(which);
            }
        });

        Dialog thisDialog = builder.create();
        thisDialog.setTitle(choiceTitle);
        return thisDialog;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

}

And for what it's worth, here's fragment_dialog_test.xml: 而其价值,这是fragment_dialog_test.xml:

<FrameLayout 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="samapps.myfirstapp.DialogTest"
    android:windowNoTitle="false"
    style="@style/CustomDialog">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

with this in "Styles.xml" 在“ Styles.xml”中

<style name="CustomDialog" parent="Theme.AppCompat.Dialog">
    <item name="windowNoTitle">false</item>
</style>

In your DialogFragment , you are using wrong KEY to get title theBundle.getString("choice_title") . DialogFragment ,您使用了错误的KEY来获取标题theBundle.getString("choice_title") Try using title_question . 尝试使用title_question

Use: 采用:

@Override
public Dialog onCreateDialog (Bundle savedInstanceState){
    Bundle theBundle = this.getArguments();

    String[] choices = theBundle.getStringArray("choices");
    String choiceTitle = theBundle.getString("title_question");
    ...........
}

could it be because you are setting the title extra with key ""title_question" but then expecting it in the dialog to be set with key "choice_title" 可能是因为您正在使用““ title_question”键设置额外的标题,但是希望在对话框中使用” choice_title“键设置标题

try changing to 尝试更改为

 public void sendMessage(View view){
       String[] text = {"Test 1","Test 2","Test 3"};
        String title = "Test Title";
        DialogFragment fragment = new DialogTest();
        Bundle args = new Bundle();
        args.putString("title_question",title);
        args.putStringArray("choices",text);
        fragment.setArguments(args);
        fragment.show(getSupportFragmentManager(),"testtest123");
        Log.d("tag","Executed after dialog");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM