0

私は自分のアプリで多くのアクティビティとクラスを作成しています。ただし、アクティビティごとにフォントサイズと色を変更する機能があります。この関数は、自身のアクティビティでテキストを変更します。他のアクティビティに移動するときは、textSizeとcolorをもう一度変更する必要があります。多くのクラスやアクティビティでTextViewを一度に変更する関数を作成するにはどうすればよいですか?

私のアプリの構造:

Main.java   main.xml/
Suad1.java  suad1.xml/
Suad2.java  suad2.xml/
Suad3.java  suad3.xml/
Suad4.java  suad4.xml/

これらの活動を一度に変えたい。ここにSuad1.classの私のコードがあります。

public void ShowDialog() {
    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
    final SeekBar seek = new SeekBar(this);
    seek.getProgress();
    seek.setMax(100);

    popDialog.setIcon(R.drawable.conp);
    popDialog.setTitle(R.string.menu_settings);
    popDialog.setView(seek);
    try {

        seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                 // TODO Auto-generated method stub
            }

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                subtitles.setTextSize(progress);
            }
        });
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    popDialog.setSingleChoiceItems(items , -1,
        new DialogInterface.OnClickListener() {
            int i;
            public void onClick(DialogInterface dialog, int item) {
                i = item;
                if(items[i] =="Blue") {
                    subtitles.setTextColor(getResources().getColor(R.color.font3));
                } else if(items[i] == "Yellow") {
                    subtitles.setTextColor(getResources().getColor(R.color.font1));
                } else if(items[i]== "Red") {
                    subtitles.setTextColor(getResources().getColor(R.color.font2));
                }
            }
        }
    );

    // Button
    popDialog.setPositiveButton("OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Suad1.this, "Your setting is complete", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        }
    );
    popDialog.create();
    popDialog.show();
}

そしてさらに2つの質問:

  1. ダイアログを終了したときにSeekBarを更新して、元に戻すことはできますか?
  2. 1つのAlertDialogに2つ以上のタイトルを含めることはできますか?
4

4 に答える 4

1

TextView text,color を変更するために複数のアクティビティで使用できるメソッドを使用して別のクラスを作成します。

public class ChangetextAttr {


public Activity context;

public ChangetextAttr(Activity context){

        this.context=context;
}

// Create an Method for Changing TextView Attributes

public void settextViewAttr(Activity activity, TextView txtView){

   txtView.setTextSize(15);
   txtView.setTextColor(activity.getResources().getColor(R.color.font1));
   //....
}

また、任意のアクティビティから、TextView 属性を設定するための settextViewAttr メソッドを使用できます。

   ChangetextAttr obj=new ChangetextAttr(Your_Activity.this);
   obj.settextViewAttr(this,any_textview_instance);
于 2013-01-01T07:28:14.620 に答える
0

TextView を拡張する (カスタマイズされたテキストビュー) クラスを作成し、テキストの色やテキストスタイルを変更するなど、テキストビューに対してやりたいことを何でも行います。すべての xml でこのクラスを使用します。クラス名 MyTextView の場合。次に、xmlは次のようになります...

   <urpackage.MyTextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
于 2013-01-01T07:23:52.567 に答える
0

Androidには、 StylesThemesを介して、ウィジェットの外観を変更するメカニズムが既に用意されています。

テキストを保持する任意のビューのテキスト サイズと色を設定するスタイルを定義する方法を次に示します。

<style name="BigColoredText">
    <item name="android:textColor">#FF0000</item>
    <item name="android:textSize">25sp</item>
</style>

そして、これを a に適用する方法は次のTextViewとおりです。

<TextView style="@style/BigColoredText" .../>

スタイルViewは、構築時に s にのみ提供できます。後で変更することはできません。ただし、次の方法で属性をtextAppearance参照するスタイルのサブセットである with をプログラムで変更できます。text

TextView v = ...;
v.setTextAppearance(R.styles.BigColoredText);

ActivityまたはApplication内のすべて のが同じスタイルを使用する (または少なくともデフォルトで使用する) 場合は、Theme必要です。TextView

<style name="Theme.MyTheme" parent="@android:style/Theme">
    <item name="android:textViewStyle">@style/BigColoredText</item>
</style>

次に、テーマをアプリケーション全体またはマニフェスト内の 1 つ以上のアクティビティに適用します。

<application or activity android:theme="@style/Theme.MyTheme" .../>

または、次の方法で実行時にプログラムで変更できます。

Activity activity = ...;
activity.setTheme(R.style.Theme_MyTheme);
于 2013-01-01T07:35:58.107 に答える
0

質問に答えるには:

1- 必要なのは 2 つのスタイル (太字と標準) であり、 を使用してテキストに使用されているスタイルを変更しView.setTextAppearanceます。Androidのドキュメントとこの回答を確認してください。

2- View から View に変更されていない値でシークバーを更新するには、たとえば共有設定でその進行状況を維持し、onResume で進行状況を設定する必要があります。

3- 代わりにカスタム ダイアログを使用することを検討してください (必要に応じて XML で定義できます)。/nまたは、タイトル文字列に複数行を含めることができます

于 2013-01-01T07:36:56.293 に答える