简体   繁体   English

将活动中定义的布局更改为xml布局

[英]Change layout defined in activity to xml layout

I am attempting to use some of Google's code from their audio capture sample code . 我试图从他们的音频捕获示例代码中使用一些Google 代码 They simplified the heck out of their code and made their layout within the class. 他们简化了他们的代码,并在课堂上进行了布局。 I want to have an actual xml layout. 我想要一个实际的xml布局。 I know how to do that part, but I would like to know how to change the code below to an onClick method and have all the functionality that is provided with it. 我知道如何做这个部分,但我想知道如何将下面的代码更改为onClick方法并具有随其提供的所有功能。

class PlayButton extends Button {      
    boolean mStartPlaying = true;   
    OnClickListener clicker = new OnClickListener() {  
        public void onClick(View v) {          
            onPlay(mStartPlaying);            
            if (mStartPlaying) {              
                setText("Stop playing");        
            } else {              
                setText("Start playing");     
            }          
            mStartPlaying = !mStartPlaying;      
        }      
    };      

    public PlayButton(Context ctx) {   
        super(ctx);           
        setText("Start playing");      
        setOnClickListener(clicker);    
    }   
}

Any help is appreciated. 任何帮助表示赞赏。

In the layout file, you'll have something like... 在布局文件中,你会有类似......

<LinearLayout>
   <Button android:id="play_button"/>
</LinearLayout>

In the activity, onCreate(), you can then do something like.. 在活动中,onCreate(),你可以做类似的事情..

OnClickListener clicker = new OnClickListener() {  
    public void onClick(View v) {          
        onPlay(mStartPlaying);            
        if (mStartPlaying) {              
            setText("Stop playing");        
        } else {              
            setText("Start playing");     
        }          
        mStartPlaying = !mStartPlaying;      
    }      
};      
Button b = findViewById(R.id.play_button);
b.setOnClickListener(clicker);

ALTERNATELY, you can also define the method in the xml layout that will be called in the Activity ... ALTERNATELY,您还可以在xml布局中定义将在Activity调用的方法...

<LinearLayout>
   <Button android:id="play_button" onclick="play"/>
</LinearLayout>

and then in the Activity you simply create a method, called play(View view) 然后在Activity你只需创建一个名为play(View view)的方法play(View view)

public void play(View view) {
            onPlay(mStartPlaying);            
            if (mStartPlaying) {              
                setText("Stop playing");        
            } else {              
                setText("Start playing");     
            }          
            mStartPlaying = !mStartPlaying;      
}

Just define the buttons as Button and declare the booleans as Activity variables. 只需将按钮定义为Button ,并将布尔值声明为Activity变量。 Example... 例...

public class AudioRecordTest extends Activity {
    ...
    private Button mPlayButton = null;
    private boolean mStartPlaying = true;
    // Do the same for mRecordButton and mStartRecording
    ...

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        // The next line assumes the play button has the id "@+id/play_button"
        mPlayButton = (Button)findViewById(R.id.play_button);
        mPlayButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);            
                if (mStartPlaying) {
                    ((Button)v).setText("Stop playing");
                } else {
                    ((Button)v).setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        });

        // Do the same for the mRecordButton
    }
}

Extending button just to set an onClickListener is not a good idea. 扩展按钮只是为了设置onClickListener不是一个好主意。 You should only extend something when you are going to add new functionality to it. 您只应在要为其添加新功能时扩展某些内容。 Not when you are going to use it for a particular purpose that does not require additional functionality. 当您打算将其用于不需要其他功能的特定用途时。

Button button = new Button(this);
button.setOnClickListener(...);

If you need to use XML, you can load it programmatically with a LayoutInflater. 如果需要使用XML,可以使用LayoutInflater以编程方式加载它。

Your boolean isPlaying is not a property of the button itself but of the media it is playing. 你的布尔值isPlaying不是按钮本身的属性,而是它正在播放的媒体的属性。 You should not hide it inside the button. 你不应该把它藏在按钮里面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM