java DialogFragment_Sample
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java DialogFragment_Sample相关的知识,希望对你有一定的参考价值。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/imgIcon"
android:gravity="center"
android:background="#DA5F6A"
android:src="@drawable/dialog_error"
android:scaleType="fitCenter"
android:contentDescription="@string/error_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_info"
android:layout_below="@+id/imgIcon"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="@string/ok"
android:id="@+id/btn_close"
android:gravity="center_vertical|center_horizontal"
android:layout_below="@+id/txt_info"
android:layout_marginBottom="20dp"
android:background="#DA5F6A"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
</RelativeLayout>
package com.example.solinari.dialogfragment;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText txtMsg;
Button btnShowDalog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMsg = (EditText) findViewById(R.id.txt_Msg);
btnShowDalog = (Button) findViewById(R.id.btn_showDialog);
btnShowDalog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment dialog =DialogFragmentHelper.newInstance(txtMsg.getText().toString());//將EditText值傳送給DialogFragment
dialog.show(getSupportFragmentManager(),"dialog");//Dialog出現
}
});
}
}
package com.example.solinari.dialogfragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Solinari on 2016/12/4.
*/
public class DialogFragmentHelper extends DialogFragment{
public static DialogFragmentHelper newInstance(String msg){
DialogFragmentHelper f = new DialogFragmentHelper();
Bundle args = new Bundle();
args.putString("msg",msg);
f.setArguments(args);//透過setArguments傳值
return f;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//取消Dialog title列
getDialog().setCanceledOnTouchOutside(false);//不能點擊Dialog以外區域
View v = inflater.inflate(R.layout.dialog, container, false);//選擇自定義的layout
Button btn = (Button) v.findViewById(R.id.btn_close) ;
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
dismiss();//Dialog關閉
}
} );
TextView txt = (TextView) v.findViewById(R.id.txt_info);
String msg = getArguments().getString("msg");
txt.setText(msg);
return v;
}
}
以上是关于java DialogFragment_Sample的主要内容,如果未能解决你的问题,请参考以下文章