私のフラグメントでは、特定のテキスト フィールドが入力されていない場合、エラー メッセージがダイアログの形式でユーザーに表示されます。ただし、エラー メッセージの長さに応じて、ダイアログ ボックスの高さをプログラムで変更する必要があります。
私のフラグメントの onCreateView() メソッドで、Dialog の xml ファイルから TextView 要素の 1 つの値を正常に出力できました。
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
View t = deetsView.findViewById(R.id.dialogText);
String val = ((TextView)t).getText().toString();
System.out.println(val);
val は、正しい値である "TeStInG" として正常に出力されます。xml ファイルは次のとおりで、android:text="TeStInG" を確認します。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/signUpFillDeets"
android:orientation="vertical"
android:layout_width="210dp"
android:layout_height="230dp"
android:background="#ffffffff">
<ImageView
android:id="@+id/crossImage"
android:layout_width="match_parent"
android:src="@drawable/dialog_cross"
android:layout_height="120dp"
android:gravity="center"
android:background="#1F1C1C"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/dialogText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/crossImage"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:text="TeStInG"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:id="@+id/dialogButton"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="OK"
android:textStyle="italic"
android:gravity="center_vertical|center_horizontal"
android:layout_below="@+id/dialogText"
android:layout_marginBottom="20dp"
android:background="@drawable/button_background_red"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
</RelativeLayout>
ただし、ID (signUpFillDeets) を介してレイアウト ファイルにアクセスしようとすると、変更は発生しません。
以下のコードはすべて onCreateView() で実行され、実際にダイアログを表示するコードは setUserVisibleHint() メソッド内にあります。したがって、論理的には、この時点でエラー メッセージの高さを設定すると、アプリケーションが閉じられるまで、ダイアログのサイズに永続的な影響が及ぶはずです。
試行 1:
deetsView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000));
試行 2:
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
結果はエラー - java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.height' on a null object reference
試行 3:
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams dimensions = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000);
layout.setLayoutParams(dimensions);
誰でもこれについて支援できますか?
更新 - onCreateView() および setUserVisibleHint()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.sign_up_fragment_third, container, false);
// we need to apply our custom font to the TextViews on our fragment. So first need to reference the TextView components via our sign_up_fragment_third view, and apply the TypeFact
View title = view.findViewById(R.id.signUpTertTitle);
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
View t = deetsView.findViewById(R.id.dialogText);
String val = ((TextView)t).getText().toString();
System.out.println(val);
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
/*
Attempt 1
deetsView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000));
Attempt 2
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
*/
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// if the user has not supplied a first name then we will display to them an prompt to enter a first name.
String firstName = ((EditText)SignUpFragmentSecond.firstName).getText().toString();
System.out.println("First name val is " + firstName);
String errorMessage = "Need to fill out ";
if(firstName.equals("")){
System.out.println("First name is empty");
errorMessage += " First Name, ";
System.out.println("Now is " + errorMessage);
}
if(!errorMessage.equals("Need to fill out ")){
SignUpActivity.pager.setCurrentItem(1);
SignUpFillDetails dialog = new SignUpFillDetails();
dialog.showDialog(getActivity(), errorMessage);
}
} else {
System.out.println("THIRD is not visible to the user");
}
}
UPDATED onViewCreated() が追加され、使用される場合に備えてインポートもリストされました
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(getContext());
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
}