设置新文本时在android中按钮复制

时间:2012-09-15 13:35:58

标签: android button actionbarsherlock

我正在使用android支持库,能够使用Froyo的片段和Sherlock扩展来显示ActionBar。

我有一个显示三个textView和一个按钮的片段,我希望能够根据Intent的内容以dinamically方式更改按钮文本。问题是,当我调用button.setText(String text)时,会出现第二个按钮。但是,我在点击每个时调用了button.getId(),并且Id是相同的。我不清楚为什么会这样,所以我会感激一些帮助。

该应用程序在Samsung Galaxy S上运行,搭载Android 2.3.3。我无法上传截屏,因为我还没有足够的声誉:(

这是代码:

FRAGMENT

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragment;
import com.parse.ParseUser;
import com.tiempoderodar.egdf.EGDF_App;
import com.tiempoderodar.egdf.R;
import com.tiempoderodar.egdf.security.Constants;

/**
 * @author Daniel Leal López
 *
 */
public class ChapterDetailsFragment extends SherlockFragment{

    private Button b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("ChapterDetailsFragment", "Created");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ParseUser user = EGDF_App.getUser(); 
        b = (Button) getSherlockActivity().findViewById(R.id.buttonWatchChapter);
        if (user != null){

            String chapterNumber = "";
            Bundle extras = getSherlockActivity().getIntent().getExtras();
            String s = extras.getString(Constants.CHAPTER_NUMBER_PARSE);
            if((s != null)&&(!s.equals(""))){
//              tvNumber.setText(s);
                chapterNumber = "Chapter_" + s;
                Log.i("Properties", s);
            }
            String seen = user.getString(chapterNumber);
            if((seen != null)&&(!seen.equals(""))&&(seen.equals(Constants.USER_CHAPTER_SEEN))){
                b.setText("Second text: "+getString(R.string.watch_chapter_button_text));
                Log.i("BUTTON CHANGED", "Text changed to watch");
            }               
            else if((seen != null)&&(!seen.equals(""))&&(seen.equals(Constants.USER_CHAPTER_NOT_SEEN))){
                b.setText("Second text: " + getString(R.string.buy_chapter_button_text));
                Log.i("BUTTON CHANGED", "Text changed to buy");
            }else{
                Log.w("DEV ERROR", "Chapter text not obtained");
            }
        }else{
            Log.w("DEV ERROR", "ParseUser is null in EGDF_App!!!");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chapter_details, container, false);
        return view;
    }

    public void setText(String item) {
        getSherlockActivity().getSupportActionBar().setTitle(item);
    }

    public void setButtonText(String text){
        b.setText(text);
    }

}

活动

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class ChapterDetailsActivity extends SherlockFragmentActivity {

    private Bundle extras;
    MediaPlayer mMediaPlayer;

    private String chapterNumber;
    private boolean isChapterSeen;
    //  private int duration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_chapter_details);

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            ChapterDetailsFragment details = new ChapterDetailsFragment();
            details.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(
                    android.R.id.content, details).commit();
        }

    }

    public void watchChapter(View view){
        Log.i("Button", "Watch chapter button PRESSED");
        Button b = (Button) view;

        Log.d("BUTTON PARENT VIEW", b.getParent().getClass().getName());
        Log.d("BUTTON ID", String.valueOf(b.getId()));
        String loadChapter = getString(R.string.load_chapter_button_text);
        String watchChapter = getString(R.string.watch_chapter_button_text);
        String buyChapter = getString(R.string.buy_chapter_button_text);

    }

    @Override
    protected void onPause() {
        Log.i("Activity lifecycle", "On pause called");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.i("Activity lifecycle", "On stop called");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.i("Activity lifecycle", "On destroy called");
        super.onDestroy();
        EGDF_App.releaseMediaPlayer();
    }

    @Override
    protected void onResume() {
        Log.i("Activity lifecycle", "On resume called");
        super.onResume();
    }

    @Override
    protected void onStart() {
        Log.i("Activity lifecycle", "On start called");
        super.onStart();
    }
}

活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/chapterDetailsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.tiempoderodar.egdf.content.ChapterDetailsFragment" />

</LinearLayout> 

片段布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewChapterNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterSeason"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterSinopsis"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterCredits"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <Button 
        android:id="@+id/buttonWatchChapter"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/watch_chapter_button_text"
        android:layout_gravity="center"
        android:onClick="watchChapter"/>

</LinearLayout>

谢谢!

1 个答案:

答案 0 :(得分:0)

我想我可能已经发现了这个问题。我在打电话

b = (Button) getSherlockActivity().findViewById(R.id.button); 

但它应该是

b = (Button) getView().findViewById(R.id.button);

通过该更改,它可以正常工作