正如文章标题,自己在进行android编程时遇到了一些问题,至于前一个是否可以跳转,自己以为是不可以的,希望可以同大家一起探讨以及如何实现,自己没有能成功实现的了。
下面是自己实现上面两个观点的代码。
package life.practise.collectionabouticr;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class radiob extends Activity implements OnCheckedChangeListener{
private RadioButton radiob1,radiob2,radiob3;
private RadioGroup radiog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.radiob);
radiob1=(RadioButton) findViewById(R.id.radiob1);
radiob2=(RadioButton) findViewById(R.id.radiob2);
radiob3=(RadioButton) findViewById(R.id.radiob3);
radiog=(RadioGroup) findViewById(R.id.radiog);
radiog.setOnCheckedChangeListener(this);/*这句是想要表达的比较核心的观点。而不是通过建立三个radiob.setonchecked...我们通常会认为在android屏幕中单击的是单选按钮,没有对所谓的RadioGroup操作,但是这里的监听在一组之中仅由组长完成监听任务就可以了*/
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(radiog==group){
String str= null;
if(checkedId==radiob1.getId()){
str=radiob1.getText().toString();
}else if(checkedId==radiob2.getId()){
str=radiob2.getText().toString();
}else if(checkedId==radiob3.getId()){
str=radiob3.getText().toString();
}
Toast.makeText(this, str+" has been selected!", Toast.LENGTH_SHORT).show();
}
}
}
一下是匿名内部类,因为没有成功实现在匿名内部类进行跳转,故此以为在匿名内部类中不可以,如果观点错误希望同大家一起探讨完成更正。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AlsoButtonWithAnonymousInnerClassActivity extends Activity {
/** Called when the activity is first created. */
private Button main_bt1,main_bt2;
private TextView main_tv1,main_tv2;
private Boolean flag1=false,flag2=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main_bt1=(Button) findViewById(R.id.bt1);
main_bt2=(Button) findViewById(R.id.bt2);
main_tv1=(TextView) findViewById(R.id.tv1);
main_tv2=(TextView) findViewById(R.id.tv2);
main_bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
main_tv1.setText("the first button has been touched!");
flag1=true;
}
});
main_bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
main_tv1.setText("the second button has been touched!");
flag2=true;
}
});
}
}
下面是自己实现上面两个观点的代码。
package life.practise.collectionabouticr;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class radiob extends Activity implements OnCheckedChangeListener{
private RadioButton radiob1,radiob2,radiob3;
private RadioGroup radiog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.radiob);
radiob1=(RadioButton) findViewById(R.id.radiob1);
radiob2=(RadioButton) findViewById(R.id.radiob2);
radiob3=(RadioButton) findViewById(R.id.radiob3);
radiog=(RadioGroup) findViewById(R.id.radiog);
radiog.setOnCheckedChangeListener(this);/*这句是想要表达的比较核心的观点。而不是通过建立三个radiob.setonchecked...我们通常会认为在android屏幕中单击的是单选按钮,没有对所谓的RadioGroup操作,但是这里的监听在一组之中仅由组长完成监听任务就可以了*/
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(radiog==group){
String str= null;
if(checkedId==radiob1.getId()){
str=radiob1.getText().toString();
}else if(checkedId==radiob2.getId()){
str=radiob2.getText().toString();
}else if(checkedId==radiob3.getId()){
str=radiob3.getText().toString();
}
Toast.makeText(this, str+" has been selected!", Toast.LENGTH_SHORT).show();
}
}
}
一下是匿名内部类,因为没有成功实现在匿名内部类进行跳转,故此以为在匿名内部类中不可以,如果观点错误希望同大家一起探讨完成更正。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AlsoButtonWithAnonymousInnerClassActivity extends Activity {
/** Called when the activity is first created. */
private Button main_bt1,main_bt2;
private TextView main_tv1,main_tv2;
private Boolean flag1=false,flag2=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main_bt1=(Button) findViewById(R.id.bt1);
main_bt2=(Button) findViewById(R.id.bt2);
main_tv1=(TextView) findViewById(R.id.tv1);
main_tv2=(TextView) findViewById(R.id.tv2);
main_bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
main_tv1.setText("the first button has been touched!");
flag1=true;
}
});
main_bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
main_tv1.setText("the second button has been touched!");
flag2=true;
}
});
}
}