3

我的 listactivity 由多行组成,每行打开活动,其中包含文本和两个按钮,其中一个打开无限画廊,另一个打开对话框,每个对话框都有不同的字符串,

我有 20 行,所以我添加了 20 次对话框,这是多余的,它的工作也很好,但我认为有比我做的更好的方法,

任何帮助将不胜感激,谢谢

我的一天课程:

public class MyDay extends Activity {
final Context context = this;
private Button button;
TextView tv1,tv2,tv3,tv4;
String day;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.Layou
          tParams.FLAG_FULLSCREEN); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    Boolean customTitleSupported =       
         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);     
    setContentView(R.layout.day);  

    if (customTitleSupported) {          
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);  } 

    initializeTextViews(); }

private void initializeTextViews() {
    tv1=(TextView)findViewById(R.id.title_tv1); 
    tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));

    tv2=(TextView)findViewById(R.id.day_tv1);
    tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));

    tv3=(TextView)findViewById(R.id.day_tv3);
    tv3.setTypeface(FontFactory.getBFantezy(getBaseContext()));

     day=getIntent().getStringExtra("cheese");

    if(day.equalsIgnoreCase("Day1")){
        tv1.setText("First Day");       
        tv2.setText(Html.fromHtml(getString(R.string.beginning)));  
        tv3.setText(Html.fromHtml(getString(R.string.day1))); 

        button = (Button) findViewById(R.id.city_button);        
        button.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
        // custom dialog
        final Dialog dialog = new Dialog(context,R.style.cust_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);           
        dialog.setContentView(R.layout.custom_dialog); 
        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
        text.setTypeface(FontFactory.getBFantezy(getBaseContext()));                
        text.setText(Html.fromHtml(getString(R.string.torusim_places_1)));

    Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);             
                dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();}
                         });
                dialog.show(); }
                         }); }

     else if(day.equalsIgnoreCase("Day2")){
        tv1.setText("Second Day");
        tv2.setText(Html.fromHtml(getString(R.string.beginning)));
        tv3.setText(Html.fromHtml(getString(R.string.day2))); 

        button = (Button) findViewById(R.id.city_button);        
        button.setOnClickListener(new OnClickListener() {    
          public void onClick(View arg0) {   
            // custom dialog
        final Dialog dialog = new Dialog(context,R.style.cust_dialog);                  
                 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);          
         dialog.setContentView(R.layout.custom_dialog);  

    TextView text = (TextView) dialog.findViewById(R.id.dialog_text);               
              text.setTypeface(FontFactory.getBFantezy(getBaseContext()));              
        text.setText(Html.fromHtml(getString(R.string.torusim_places_2)));

    Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);         
         dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    dialog.dismiss(); }
                            });  
                    dialog.show(); }
                        }); }
     else if(day.equalsIgnoreCase("Day3")){
        tv1.setText("Third Day");
        tv2.setText(Html.fromHtml(getString(R.string.beginning)));
        tv3.setText(Html.fromHtml(getString(R.string.day3))); 

        button = (Button) findViewById(R.id.city_button);        
        button.setOnClickListener(new OnClickListener() {    
          public void onClick(View arg0) {   
        // custom dialog
        final Dialog dialog = new Dialog(context,R.style.cust_dialog);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);           
                dialog.setContentView(R.layout.custom_dialog);                  
        TextView text = (TextView) dialog.findViewById(R.id.dialog_text);               
                  text.setTypeface(FontFactory.getBFantezy(getBaseContext()));              
               text.setText(Html.fromHtml(getString(R.string.torusim_places_3)));

    Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);             
            dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
            // if button is clicked, close the custom dialog
        ialogButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    dialog.dismiss(); }
                            });  
                    dialog.show(); }
                        }); 
                                   }
                                 }

  // this continuing repeated till day 20 // 

 public void handleClick(View v){

    //Create an intent to start the new activity.

    Intent intent = new Intent();
    intent.setClass(this,DayGallery.class);
    intent.putExtra("dayname",day);
    startActivity(intent);

                  }
            }
4

2 回答 2

1

你有很多重复的代码。我建议将指示额外类型的意图日更改为 int,并将您的文本资源放入索引 = 天数 - 1 的数组中。

此外,如果您在活动类中实现 onClickListener 接口而不是创建新对象,则更容易阅读和维护代码。

这是您的代码的重构版本,更短更清晰:

public class MyDay extends Activity implements View.OnClickListener {
    private Context mContext = this;
    private Button mButton;
    private TextView tv1, tv2, tv3, tv4;
    private int  dayNumber;
    private Dialog mDialog;
    // this string should be in resources, like other below
    private final String[] tv1Resources = {"First day", "Second day", "Third day" , ...}; 
    private final int[] tv3Resources = {R.string.day1, R.string.day2, R.string.day3 , ...};
    private final int[] dialogTextResources = {R.string.torusim_places_1, R.string.torusim_places_2, R.string.torusim_places_3 , ...};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Boolean customTitleSupported =       
                requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);     
        setContentView(R.layout.day);  

        if (customTitleSupported) {          
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);  } 

            // note, now you should put integer extra, 
            //not String, this extra is just day number -1, 
            //for day1 this is 0, for day2 this is 1 etc. 
            //In your case, probably this number == item position in listview of 
        dayNumber = getIntent().getIntExtra("cheese", -1); previous activity
        if (dayNumber == -1){
            finish(); //this needed if somehow you get invalid extra
            return;
        }

        initializeTextViews();
    }

    @Override
    public void onClick(View v) {       
        switch (v.getId()){
            //actually this switch isn't needed because you have 
            //only one button in this activity, but I wanted to show,
            // how you can maintain many onClick events here
        case R.id.city_button: 
            // custom dialog
                    // we use class field for avoid making final dialog local variable
            mDialog = new Dialog(mContext, R.style.cust_dialog); 

            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            mDialog.setContentView(R.layout.custom_dialog);
            // set the custom dialog components - text, image and button
            TextView text = (TextView) mDialog.findViewById(R.id.dialog_text);
            text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
            text.setText(Html.fromHtml(getString(dialogTextResources[dayNumber])));

            Button dialogButton = (Button) mDialog.findViewById(R.id.dialog_Button);
            dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    if (mDialog != null) mDialog.dismiss(); // check for aviod dialog null pointer
                }
            });
            mDialog.show(); 
            break;
        }
    }

    private void initializeTextViews() {
        tv1 = (TextView) findViewById(R.id.title_tv1);
        tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));

        tv2 = (TextView) findViewById(R.id.day_tv1);
        tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));


        tv3 = (TextView) findViewById(R.id.day_tv3);
        tv3.setTypeface(FontFactory.getBFantezy(getBaseContext()));


        mButton = (Button) findViewById(R.id.city_button);
        mButton.setOnClickListener(this);

        tv1.setText(tv1Resources[dayNumber]);
         // due to code, this resource is the same for all days
        tv2.setText(Html.fromHtml(getString(R.string.beginning)));
        tv3.setText(Html.fromHtml(getString(tv3Resources[dayNumber])));
    }

    public void handleClick(View v) {
        // Create an intent to start the new activity.
        Intent intent = new Intent();
        intent.setClass(this, DayGallery.class);
        intent.putExtra("dayname", day);
        startActivity(intent);
    }
}

希望这可以帮助。

于 2013-06-02T21:14:37.733 回答
0

您可以将对话框创建放在一个单独的方法中,然后从您onClick的按钮中调用该方法。类似于以下内容:

if(day.equalsIgnoreCase("Day1")){
    tv1.setText("First Day");       
    tv2.setText(Html.fromHtml(getString(R.string.beginning)));  
    tv3.setText(Html.fromHtml(getString(R.string.day1))); 

    button = (Button) findViewById(R.id.city_button);        
    button.setOnClickListener(new OnClickListener() { 
        public void onClick(View arg0) { 
            dialogCreation(Html.fromHtml(getString(R.string.torusim_places_1)).toString());
        }

 else if(day.equalsIgnoreCase("Day2")){.... }

然后你dialogCreation()可能看起来像:

public void dialogCreation(String arg0) {
     // custom dialog
    final Dialog dialog = new Dialog(context,R.style.cust_dialog);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);           
    dialog.setContentView(R.layout.custom_dialog); 
    // set the custom dialog components - text, image and button
    TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
    text.setTypeface(FontFactory.getBFantezy(getBaseContext()));                
    text.setText(arg0);

     Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);             
            dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            dialog.dismiss();}
                     });
            dialog.show(); }
                     }); 
}
于 2013-05-20T18:07:45.297 回答