2012-11-29 78 views
0

的android我嘗試做微調和按鈕,clickHandler事件微調和按鈕與函數clickhandler

這是我nyoba.java

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class nyoba extends Activity { 
    String[] nama_hari = {"Senin", "Selasa", "Rabu","Kamis","Jum'at","Sabtu","Minggu"}; 
    /** Called when the activity is first created. */ 
    Spinner spinner1; 
    Button btnSubmit; 
    TextView textView1; 
    String tes; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     btnSubmit = (Button)findViewById(R.id.btnSubmit); 
     textView1 = (TextView)findViewById(R.id.textView1); 
     spinner1 = (Spinner)findViewById(R.id.spinner1); 
     ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, nama_hari); 
      aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      spinner1.setAdapter(aa); 
    } 
    public void onItemSelected(AdapterView<?> arg0, View arg1, 
      int arg2, long arg3) { 
      tes = nama_hari[arg2]; 
    } 
    public void clickHandler(View view){ 
     switch (view.getId()){ 
     case R.id.btnSubmit: 
     textView1.setText(tes); 
      break; 
} 
    } 
} 

,這是我的main.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:background="#ffffff"> 



     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dip" 
      > 

      <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/btnSubmit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="clickHandler" 
     android:text="Submit" /> 

    <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       /> 


     </LinearLayout> 


    </RelativeLayout> 
</ScrollView> 

但是當我運行它到模擬器,並且我在微調器中選擇1個選項並單擊該按鈕時,TextView不顯示來自我選擇的微調器的單詞

(例如,我選擇「senin」的微調,然後按一下按鈕,但文本「senin」不TextView的出現)

我檢查DDMS並沒有錯誤....

是代碼錯誤? 你能幫我請哪一個是錯的?

感謝反正....

回答

0

我覺得你不設置選擇監聽器上的微調。

但實際上你可以檢索微調的選定位置getSelectedItemPosition在按鈕的點擊處理程序:

試試這個:

public void clickHandler(View view) { 
    switch (view.getId()) { 
    case R.id.btnSubmit: 
     textView1.setText(nama_hari[spinner1.getSelectedItemPosition()]); 
     break; 
    } 
} 
+0

另外'onItemSelected'沒有被綁定到了微調,這爲什麼'tes'是空的。 – dmon

+0

啊,你剛剛編輯你的帖子說同樣的事情:) – dmon