2017-06-05 101 views
-4
private Button mfactbutton; 
private TextView mfacttext; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my_fun_fact); 

    Button mfactbutton = (Button) findViewById(R.id.button); 
    TextView mfacttext = (TextView) findViewById(R.id.textView2); 

    // now we need to make out button to click 
    View.OnClickListener Listener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String[] facts = { 
        "Ants stretch when they wake up in the morning.", 
        "Ostriches can run faster than horses.", 
        "Olympic gold medals are actually made mostly of silver.", 
        "You are born with 300 bones; by the time you are an adult you will have 206.", 
        "It takes about 8 minutes for light from the Sun to reach Earth.", 
        "Some bamboo plants can grow almost a meter in just one day.", 
        "The state of Florida is bigger than England.", 
        "Some penguins can leap 2-3 meters out of the water.", 
        "On average, it takes 66 days to form a new habit.", 
        "Mammoths still walked the earth when the Great Pyramid was being built." }; 

      String fact = ""; 

      // randomly select a fact 

      Random randomGenerator = new Random(); 
      int randomNumber = randomGenerator.nextInt(facts.length); 
      fact = facts[randomNumber] + ""; 
     } 
    }; 
    mfactbutton.setOnClickListener(Listener); 


} 

}按鈕不點擊在Android Studio中

大家好!我需要幫助!我的按鈕不僅僅是簡單的! heeeeeeeeelp!我只是試圖做一個簡單的按鈕,每次點擊改變textview2!起初它工作,但現在它開始不工作。

+2

因爲你沒有用'mfacttext'做任何事情,所以在這行之後加上'mfacttext.setText(fact)''fact = facts [randomNumber] +「」;' –

+1

@Pavneet_Singh究竟是什麼意思 –

回答

0

Button點擊正常,但最主要的是你沒有fact值設置爲TextView

#。由於您已在onCreate()之外聲明ButtonTextView,因此無需在onCreate()內再次聲明它。

用途:

mfactbutton = (Button) findViewById(R.id.button); 
    mfacttext = (TextView) findViewById(R.id.textView2); 

相反的:

Button mfactbutton = (Button) findViewById(R.id.button); 
    TextView mfacttext = (TextView) findViewById(R.id.textView2); 

#。onClick()方法顯示在TextViewfact值或顯示Toast消息:

// TextView 
    mfacttext.setText(fact); 

    // Toast 
    Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show(); 

下面是工作的代碼:

public class MainActivity extends AppCompatActivity { 

    private Button mfactbutton; 
    private TextView mfacttext; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mfactbutton = (Button) findViewById(R.id.button); 
     mfacttext = (TextView) findViewById(R.id.textView2); 

     // now we need to make out button to click 
     View.OnClickListener Listener = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String[] facts = { 
         "Ants stretch when they wake up in the morning.", 
         "Ostriches can run faster than horses.", 
         "Olympic gold medals are actually made mostly of silver.", 
         "You are born with 300 bones; by the time you are an adult you will have 206.", 
         "It takes about 8 minutes for light from the Sun to reach Earth.", 
         "Some bamboo plants can grow almost a meter in just one day.", 
         "The state of Florida is bigger than England.", 
         "Some penguins can leap 2-3 meters out of the water.", 
         "On average, it takes 66 days to form a new habit.", 
         "Mammoths still walked the earth when the Great Pyramid was being built." }; 

       String fact = ""; 

       Random randomGenerator = new Random(); 
       int randomNumber = randomGenerator.nextInt(facts.length); 

       fact = facts[randomNumber] + ""; 

       mfacttext.setText(fact); 
       Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show(); 
      } 
     }; 

     mfactbutton.setOnClickListener(Listener); 
    } 
} 

OUTPUT:

enter image description here

0

你是否將你選擇的事實設置爲你的textView?添加

mfacttext.setText(fact); 

選擇您的隨機事實後。

Random randomGenerator = new 
int randomNumber = randomGenerator.nextInt(facts.length); 
fact = facts[randomNumber] + ""; 

mfacttext.setText(fact); 
0

使用本

mfactbutton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
// Your code goes here 
    } 
}); 
0

嗨,我剛剛創建了一個簡單的項目。如果你點擊按鈕,它將在textview2上生成隨機文本。

MainActivity.java

public class MainActivity extends AppCompatActivity { 
TextView secondTextView; 
String[] Textlist = { "Hello man ", "sonam", "tashi","i am man","hellow world"}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    secondTextView = (TextView) findViewById(R.id.textView2); 

    Button mybtn = (Button) findViewById(R.id.btn); 
    mybtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      changeTextView_two_text(); 

     } 
    }); 
} 

private void changeTextView_two_text() { 

    Random random = new Random(); 
    String rand = Textlist[random.nextInt(Textlist.length)]; 
    // String randomText = TextList[random.nextInt(TextList.length)]; 

    secondTextView.setText(rand); 


} 

}

和activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.example.hello.googlemap.MainActivity"> 
<Button 
    android:id="@+id/btn" 
    android:text="click me" 
    android:layout_width="368dp" 
    android:layout_height="wrap_content" 
    tools:layout_editor_absoluteY="0dp" 
    tools:layout_editor_absoluteX="8dp" /> 
<TextView 
    android:gravity="center" 
    android:id="@+id/textView2" 
    android:text="no text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/></LinearLayout> 

希望這有助於你。