使用按钮在微调器上执行不同的任务单击一个按钮

时间:2015-01-16 19:49:49

标签: android buttonclick

我想构建一个使用微调器和按钮的简单Android应用程序。微调器包含两个项目。 ITEM1->谷歌 ITEM2->雅虎 我想要: 如果我从微调器中选择Item1(Google)并单击按钮,它会在Android中的WebView中重定向到google.com,并且 如果我从微调器中选择Item2(Yahoo)并单击相同的按钮,它将重定向到yahoo.com。

这是我的xml代码:

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

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="158dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="72dp"
        android:onClick="onClick"
        android:text="@string/buttn" />

</RelativeLayout>

值项目为微调器:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="section">
        <item>Google</item>
        <item>Yahoo</item>

    </string-array>
</resources>

Java代码:

public class FirstPage extends ActionBarActivity implements OnItemSelectedListener
{


        Spinner sp;
        Button buttn;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.firstpage);
            buttn= (Button) findViewById(R.id.button1);
            sp = (Spinner) findViewById(R.id.spinner1);
            ArrayAdapter<CharSequence> ar = ArrayAdapter.createFromResource(this, R.array.section, android.R.layout.simple_list_item_1);
            ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            sp.setAdapter(ar);

            addListenerOnButton();
        }


        private void addListenerOnButton() 
        {
            buttn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    try{
                    sp = (Spinner) findViewById(R.id.spinner1);
                    buttn= (Button) findViewById(R.id.button1);
                    }
                }
            });

        }

            public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) {

            }

            public void onNothingSelected(AdapterView<?> 
            }
        }

我不知道该怎么做。 有人请帮帮我。 感谢。

2 个答案:

答案 0 :(得分:0)

如果你问如何开始浏览,而你的听众已经在工作,那么我会试试这个:

   Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    startActivity(browse);

您还可以see more questions about your problem

此外,微调器对onClickListener效果不佳,如this post所示,我建议使用其他方法,例如OnItemSelectedListener()。

答案 1 :(得分:0)

这样做

private void addListenerOnButton() 
{
    buttn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String val = sp.getSelectedItem().toString();
            String url = "";
            if(val.equalsIgnoreCase("google")){
                url = "http://www.google.com";
            else if (val.equalsIgnoreCase("yahoo"))
                url = "http://www.yahoo.com"; 

            if(url.length() > 0){
                Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                YourActivity.this.startActivity(browse);
            } 
        }
    });
}

修改

要在您的活动网页浏览中打开网址,请执行此操作

Intent intent = new Intent(YourCurrentActivity.this, MyWebViewActivity.class); // Change the activity name as yours
intent.putExtra("url", url);
YourCurrentActivity.this.startActivity(intent); // Change the activity name as yours

在webview

的下一个活动中执行此操作
String url = getIntent().getStringExtra("url");
WebView webView = (WebView) findViewById(R.id.web_view); // Change to your id
webView.loadUrl(url);