ボタン上のテキストを更新します
-
21-12-2019 - |
質問
こんにちは私はなぜ私のアプリで異なる振る舞いをするのかを更新する理由が疑問に思いました。
私は、ボタンを更新する必要があるものを取得するフラグメントを呼び出して、断片からのインタフェースコールバックを実行して、アクティビティがボタンテキストを更新するグローバル変数を更新します。
問題は、ボタンが新しいテキストを表示するためにボタンを更新しないようにするが、それが機能するフラグメントを通してそれを実行した場合、ボタンを無効にしたり、それを強制したりしない場合活動を通して。
これは、リストを表示した断片を呼び出しているときとボタンをクリックすると、選択したときのボタンを押すと、選択したオプションをクリックしたときのアクティビティがあります。
public void onClick_testSite(View view)
{
// create the fragment
SelectChartTypeDialogFragment chartType = new SelectChartTypeDialogFragment();
// register you as a delegate for the callback
chartType.delegate = this;
// show the list
chartType.show(getFragmentManager(), "WEE");
// fetch the button and set the text ** DOESNT WORK **
Button p1_button = (Button)findViewById(R.id.btn_pickChart);
p1_button.setText(response);
p1_button.invalidate();
}
@Override
public void processFinish(String response)
{
this.response = response;
}
.
とここではダイアログを処理するフラグメントの一部です:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// get the list from a enum
final List<ChartTypes> chartList = Arrays.asList(ChartTypes.values());
// The array containing the different choices
ArrayAdapter<ChartTypes> adapter = new ArrayAdapter<ChartTypes>(
getActivity(), android.R.layout.simple_list_item_1, chartList);
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// The dialog setting
builder.setTitle(R.string.pick_chart);
builder.setAdapter(adapter, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int position)
{
// get the name of the enum based on the position, what one
// clicked on the dialog
String strName = chartList.get(position).name();
// this sets the text in the activitys global variable
delegate.processFinish(strName);
// set the text in the fragment instead
//changeBtnText(strName);
//dialog.dismiss();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
public void changeBtnText(String newBtnxt)
{
Button button = (Button)getActivity().findViewById(R.id.btn_pickChart);
button.setText(newBtnxt);
}
.
私の質問は、(アプリを実行するときに)フラグメントを介して(アプリを実行するとき)、つまりアクティビティを通過しないようなテキストを更新するのですが、i.eメソッドp1_button.settext(応答);?
Raghunandanによって説明されているように答えを編集する:
問題は、ダイアログで何でもクリックしなかったとしても、onClick_testSite(View view)
が完了したことがわからなかったため、chartType.show()
の関数呼び出しを返し、次に機能の最後に進みました。
解決
public AsyncResponse delegate = null;
delegate =(AsyncResponse) getActivity();
.
私はこれが実装された方法
です@Override
public void processFinish(String response)
{
Log.i(".........",response); // check if the response is logged
// if you get the response your button text will be changed
// else you need to look at why the response is not logged.
p1_button.setText(response);
}
.
instance変数としてのButton p1_button
を宣言します(たとえば、oncreateの前に)。
この
を初期化しますp1_button = (Button)findViewById(R.id.btn_pickChart); // in onCreate
.
processFinish
で応答を受信する前でもボタンが更新されます。その後、応答変数を初期化します。そのため、ボタンをクリックすると、ボタンのテキストが設定されます。
閉じ込め前のボタンを宣言して、上のprocessFinish
Pethantas Hwonで更新できます。
他のヒント
processfinish 値を応答に保存します。しかし、 btn_pickchart idを含むTextViewには適用されません。
だから、TextViewのインスタンスをアクティビティのインスタンスを保存する必要があります。
private Button mP1Button;
protected void onCreate(Bundle savedInstanceState) {
...
Button mP1Button = (Button) findViewById(R.id.btn_pickChart);
...
}
.
processfinish が呼び出されたときに変更された値を適用します:
public void processFinish(String response) {
this.response = response;
// The value will be setup to the view element.
mP1Button.setText(response);
}
.
最良の方法では、代理人として代理人を使用しないでください。getActivity()またはgetTargetFragment()を使用して、asyncResponseのInstanceをチェックし、ProcessFinishメソッドを呼び出します。
AsyncResponse asyncResponse = null;
if(getActivity() instanceof AsyncResponse) {
asyncResponse = (AsyncResponse) getActivity();
} else if (getTargetFragment() instanceof AsyncResponse){
asyncResponse = (AsyncResponse) getTargetFragment();
}
if(asyncResponse != null){
asyncResponse.processFinish(strName);
}
.
ところで、 settext の後に invalidate メソッドを呼び出す必要はありません。